Ruby HTTP don't for get use_ssl

In a project this week I had send some http requests from a ruby program and do something with the response. One of the requests needed to be https so I tried something along these lines…

require "net/https"
require "uri"

uri = URI.parse("https://secure.com/")
http = Net::HTTP.new(uri.host, uri.port)
response = http.request(request)


This looks like it should work right? When URI parses the https url it even knows to use the https port…

uri.port
# => 443


Only the request kept timing out and I didn’t know why. After some googleling and rereading parts of blog posts I had previously overlooked my salvation came from a very helpful post by August Lilleaas. Even though the parsed URI object recognized the https port the http request still has to be told to send as https. You can set this with an option called “user_ssl”

http.use_ssl = true


No more ambiguous timout :)

© 2013 Lupine Software Development