Wednesday, July 16, 2008

Net:HTTP gotchas

Net::HTTP.new is generally not what you want because it doesn't actually establish a connection.
Net::HTTP.start does, and seems basically strictly superior, although having both is probably nice for some niche.

The argument to Net::HTTP.start (or new) shouldn't have a preceding http://

If it does, you'll get this error.

SocketError: getaddrinfo: Name or service not known
from /usr/lib/ruby/1.8/net/http.rb:560:in `initialize'
from /usr/lib/ruby/1.8/net/http.rb:560:in `open'
from /usr/lib/ruby/1.8/net/http.rb:560:in `connect'
from /usr/lib/ruby/1.8/timeout.rb:48:in `timeout'
from /usr/lib/ruby/1.8/timeout.rb:76:in `timeout'
from /usr/lib/ruby/1.8/net/http.rb:560:in `connect'
from /usr/lib/ruby/1.8/net/http.rb:553:in `do_start'
from /usr/lib/ruby/1.8/net/http.rb:548:in `start'
from /usr/lib/ruby/1.8/net/http.rb:440:in `start'
from (irb):45


And then you'll go on a wild-goose chase around the internet, look at sample code for Net::HTTP in chapter 3 of the poignant guide and realize why it was breaking. And then write a blog post about it.

No comments: