4

How can I set a larger timeout in net/http? What I'm doing is this:

rta = JSON.parse(Net::HTTP.get(URI(url)))

I've tried:

uri = URI(url)
http = Net::HTTP.new(uri.host, uri.port)
http.open_timeout = 5* 60
http.read_timeout = 5* 60
rta = JSON.parse(Net::HTTP.get(URI(url)))

but it still doesn't work.

CDub
  • 13,146
  • 4
  • 51
  • 68
mar_sanbas
  • 833
  • 2
  • 12
  • 20
  • 1
    Maybe overwrite the initializer for Net::HTTP? See: http://stackoverflow.com/questions/2229194/ruby-nethttp-time-out – CDub Oct 11 '13 at 19:33

2 Answers2

3

It looks like it probably isn't working because you're making your get call on the Class instead of the instance you created. Try changing that last line to:

rta = JSON.parse(http.get(URI(url)))
jasonyork
  • 251
  • 2
  • 6
2

Maybe you can use OpenURI :

require 'open-uri'
open(url, :read_timeout => 5 * 60) do |file|
  rta = JSON.parse file
  # ...
end
Baldrick
  • 23,882
  • 6
  • 74
  • 79