How to specify a read timeout for a Net::HTTP::Post.new request in Ruby 2

Solved via this stackoverflow answer

I've changed my

response = Net::HTTP.start(url.host, url.port) {|http| http.request(request)}

line to be

response = Net::HTTP.start(url.host, url.port, :read_timeout => 500) {|http| http.request(request)}

and this seems to have got around this problem.


The read_timeout is available with a plain Net::HTTP object:

url = URI.parse('http://google.com')

http = Net::HTTP.new(url.host, url.port)
http.read_timeout = 5 # seconds

http.request_post(url.path, JSON.generate(params)) do |response|
  # do something with response
  p response
end