I have Googled this to death and gone through the related posts on this question. I don't think it's a Rails / Ruby issue but not sure where to go next.
irb(main):007:0> Net::HTTP.get_response("http://www.apple.com","")
SocketError: getaddrinfo: nodename nor servname provided, or not known
from /Users/dan/.rbenv/versions/2.2.3/lib/ruby/2.2.0/net/http.rb:879:in `initialize'
No network issues otherwise. apple.com loads fine in a browser. This is in a rails app in development on my local machine. Looking to pull a JSON request via a third party API. All Net::HTTP requests film like this.
You have to pass an URI object to the get_response
method so that it has scheme
and host
etc. info in it:
URI('http://www.apple.com').host
# => "www.apple.com"
URI('http://www.apple.com').scheme
# => "http"
So, to solve your problem, do it this way:
Net::HTTP.get_response(URI("http://www.apple.com"))
# => #<Net::HTTPOK 200 OK readbody=true>
To get the response body, do this:
Net::HTTP.get_response(URI("http://www.apple.com")).body
See the get_response documentation for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With