Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Net::HTTP SocketError: getaddrinfo: nodename nor servname provided, or not known

Tags:

ruby

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.

like image 768
Dan Tappin Avatar asked Oct 25 '15 03:10

Dan Tappin


1 Answers

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.

like image 83
K M Rakibul Islam Avatar answered Nov 27 '22 13:11

K M Rakibul Islam