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

后端 未结 5 1104
生来不讨喜
生来不讨喜 2021-01-01 13:26

I have a post happening to a rails application from a ruby script. The script creates a variable request as

request = Net::HTTP::Post.new(url.path)
         


        
5条回答
  •  轮回少年
    2021-01-01 14:12

    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
    

提交回复
热议问题