Syl
Syl

Reputation: 3821

I cannot use HTTP/net on my dev machine

Here is the code I am running in my rails console

http = Net::HTTP.new("http://google.fr",80)
req = Net::HTTP::Post.new("/stupid/url/")
res = http.request(req)

It makes the following error :

SocketError: Failed to open TCP connection to http://google.fr:80
(getaddrinfo: Name or service not known)

I have the same kind of error in my rails app when I try to query an API. From the same computer I can browse google. Why is it not working ?

EDIT : Same error in IRB with a require "net/http"

Upvotes: 0

Views: 89

Answers (1)

DonPaulie
DonPaulie

Reputation: 2214

Docs says that the address should be hostname or IP address. You are adding protocol. Or you can use URI class.

Working example (returning 404):

http = Net::HTTP.new("google.fr",80)
req = Net::HTTP::Post.new("/stupid/url/")
res = http.request(req)

Or you can try:

http = Net::HTTP.new(URI("http://google.fr").host)
req = Net::HTTP::Post.new("/stupid/url/")
res = http.request(req)

Upvotes: 3

Related Questions