0

I intend to send GET request with net/http just like that:

require 'net/http'
response = Net::HTTP.get_response("example.com", "/whoareyou.json")

but I need to include key and secret in the url. I tried to make it like this:

require 'net/http'
response = Net::HTTP.get_response("<key>:<secret>@example.com", "/whoareyou.json")

But unfortunately I'm getting nasty error:

/usr/lib/ruby/1.9.1/net/http.rb:762:in `initialize': getaddrinfo: Name or service not known (SocketError)
Leo
  • 2,061
  • 4
  • 30
  • 58

1 Answers1

1

Net::HTTP does not support basic authentification from the URL. You need to call req.basic_auth 'user', 'pass'

http://ruby-doc.org/stdlib-2.1.3/libdoc/net/http/rdoc/Net/HTTP.html#class-Net::HTTP-label-Basic+Authentication

I suggest also tu use the HTTP Party gem ( https://github.com/jnunemaker/httparty ) that really simplify most of the HTTP stuff , see this answer for instance to see how basic auth can be used with HTTP party : How to use basic authentication with httparty in a Rails app?

In your example:

res = HTTParty.get("http:example.com/whoareyou.json", :basic_auth=> {:username =>user, :password => password})
                 :basic_auth => auth)
Community
  • 1
  • 1
tomsoft
  • 4,448
  • 5
  • 28
  • 35