How to Specify a Read Timeout for a Net::Http::Post.New Request in Ruby 2

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.

Set custom timeout in Net::HTTP::Get.new with Rails

You need to set the read_timeout attribute.

link = URI.parse(url)
request = Net::HTTP::Get.new(link.path)
begin
response = Net::HTTP.start(link.host, link.port) {|http|
http.read_timeout = 100 #Default is 60 seconds
http.request(request)
}
rescue Net::ReadTimeout => e
puts e.message
end

Ruby 2.3 - Adding Timeout error and notification to net:http request

That's not very beautiful solution (see: https://medium.com/@adamhooper/in-ruby-dont-use-timeout-77d9d4e5a001), but I believe you still can use it here, because you only have one thing happening inside opposite to the example in the link, where multiple actions could cause non-obvious behavior:

def get_url_http_response(url)
uri = URI.parse(URI.encode(url))
request = Timeout.timeout(25) { Net::HTTP.get_response(uri) }
return request
rescue Errno::ECONNREFUSED, SocketError => e
OpenStruct.new(code: -1)
rescue Timeout::Error
# return here anything you want
end

Set read_timeout for the service call in Ruby Net::HTTP.start

If you use Ruby 1.8 you have to use Net::HTTP.new:

http = Net::HTTP.new(host, port)
http.read_timeout = 10

response = http.get("/")
# or intead of above line if you need block
# http.start do
# response = http.get("/")
# end

If you look at the source code of Net::HTTP.start, you will see that ::start is just calling ::new with #start method:

# File net/http.rb, line 439
def HTTP.start(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil, &block) # :yield: +http+
new(address, port, p_addr, p_port, p_user, p_pass).start(&block)
end

>= Ruby 1.9

You can set read_timeout in opt argument:
start(address, ..., opt, &block)

like this:

res = Net::HTTP.start(host, port, :read_timeout => 10)

How to set timeout for Net::HTTP.start?

As Ascar pointed out, this is wrong syntax.
But :read_timeout alone doesnt fix the issue, I also needed to add :open_timeout if the host didnt respond at all.

Net::HTTP.start(uri.host, uri.port, {read_timeout: 5, open_timeout: 5})

Timeout error when making get request on specific site using ruby 2.5.3

They're using akamai, so first - they expect HTTP/2 (you'll need an http2 gem for that) and they have some fairly tight User-Agent sniffing.

Here's an example that works using net-http2

client = NetHttp2::Client.new "https://www.coupang.com/"
res = client.call :get, '/', headers: { "User-Agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6)" }
puts res.body if res.ok?

Ruby Net::HTTP second request when timeout

It's a feature of Net::HTTP that it retries idempotent requests. You can limit number of retries by setting max_retries (in your case to 0).

More on that issue on Ruby redmine

require "net/http"

http = Net::HTTP.new("timeout.free.beeceptor.com", 443)
http.read_timeout = 1
http.max_retries = 0 # <<<<<<<< the change
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http.request(Net::HTTP::Get.new("/Time"))


Related Topics



Leave a reply



Submit