-
-
Save wilmoore/d8f05df279bc1045f467 to your computer and use it in GitHub Desktop.
POST JSON document, basic auth, and HTTPS (Ruby Net::HTTP)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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