1

I am trying to create a function with net/http for below curl commands

curl -H 'Authorization: Bearer OAuth2-token' https://stage.api.example.com/ssoclient/users/v1/domain_client curl -H 'content-type: application/JSON' -d @example.JSON OAuth2-url

I am able to create a function using rest-client like below RestClient.get 'OAuth2-url', {'Authorization' => 'Bearer OAuth2-auth'}

I get the expected JSON object when i use rest-client, how can i achieve the same with net/http.

Thanks,

Tommy
  • 277
  • 1
  • 4
  • 17

1 Answers1

1
url = URI.parse("https://stage.api.example.com/ssoclient/users/v1/domain_client")
req = Net::HTTP::Get.new(url.path)
req.add_field("Authorization", "Bearer OAuth2-auth")
res = Net::HTTP.new(url.host, url.port).start do |http|
  http.request(req)
end
Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Thanks amadan for your reply, but i see EOF error in res.body. the issue is the response shows 401 error on res.inspect. i was able to authenticate with curl command and rest-client library using custom headers but its not working with net/http – Tommy Mar 15 '16 at 08:11
  • http://stackoverflow.com/questions/11269224/ruby-https-post-with-headers is not working for me. Please keep this thread alive for further discussions. – Tommy Mar 15 '16 at 08:13
  • Both my code (`#add_field`) and the linked code (`Get.new`) will correctly add a custom header to the request. You can verify it by setting up a server and echoing back the headers (or just pointing your code at any phpinfo page). If you still have problems, it is not "how to do curl custom header request with net/http ruby". – Amadan Mar 15 '16 at 08:25