Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Created November 25, 2014 21:22
Show Gist options
  • Select an option

  • Save wilmoore/d8f05df279bc1045f467 to your computer and use it in GitHub Desktop.

Select an option

Save wilmoore/d8f05df279bc1045f467 to your computer and use it in GitHub Desktop.
POST JSON document, basic auth, and HTTPS (Ruby Net::HTTP)
require 'net/http'
require 'uri'
require 'json'
def question
uri = URI.parse("https://example.com/questions")
request = Net::HTTP::Post.new uri.request_uri
# basic auth.
request.basic_auth("user", "pass")
# set headers.
request["Content-Type"] = "application/json"
request["X-SyncTimeout"] = "-1"
# set data.
request.body = {"value" => "How do I lose weight?", "predefinedAnswerUnit" => "Body Composition : Weight Loss", "state" => "APPROVED"}.to_json
# send request.
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
http.request request
end
JSON.parse response.body
end
puts question
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment