10

To do a Net::HTTP https request without the block form you can do this:

...
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
...

But is there a way to tell Net::HTTP to use https when doing the block form?

u = URI.parse(url)
Net::HTTP.start(u.host, u.port) do |http|
  # if I put http.use_ssl = true here, ruby complains that this can't
  # be done becuase the sesion has already started
  resp = http.get(u.request_uri)
end

I'm on ruby 1.8.7

John Bachir
  • 22,495
  • 29
  • 154
  • 227

1 Answers1

27

See the documentation for Net::HTTP.start which takes an optional hash. From the documentation:

opt sets following values by its accessor. The keys are ca_file, ca_path, cert, cert_store, ciphers, close_on_empty_response, key, open_timeout, read_timeout, ssl_timeout, ssl_version, use_ssl, verify_callback, verify_depth and verify_mode. If you set :use_ssl as true, you can use https and default value of verify_mode is set as OpenSSL::SSL::VERIFY_PEER.

Net::HTTP.start(url.host, url.port, :use_ssl => true)
Lee Jarvis
  • 16,031
  • 4
  • 38
  • 40
  • 1
    Forgot to mention, I'm on [1.8.7](http://rubydoc.info/stdlib/net/1.8.7/Net/HTTP.start) – John Bachir Jun 14 '11 at 19:18
  • @John Then you can't do it. Ruby 1.8.7 doesn't have method signatures for this. Unfortunately you won't be able to use the block form if you want to set `use_ssl`. You can of course you can patch the 1.9.2 version of start in: https://gist.github.com/2cdc187fa0c7b608fe2c – Lee Jarvis Jun 14 '11 at 21:57