# Net::HTTP reponse method - custom timeout

**URL:** <https://www.ruby-forum.com/t/net-http-reponse-method-custom-timeout/121492>\
**Category:** Rails\
**Created:** [November 28, 2007, 1:22pm UTC](https://www.ruby-forum.com/t/net-http-reponse-method-custom-timeout/121492 "2007-11-28T13:22:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![reggieb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3e96dc/32.png) [@reggieb](https://www.ruby-forum.com/u/reggieb)\
**Post date:** [November 28, 2007, 1:22pm UTC](https://www.ruby-forum.com/t/net-http-reponse-method-custom-timeout/121492/1 "2007-11-28T13:22:14Z")

</div>

I am using the following code to control an external device via it’s  
in-built web server interface:

def send\_to\_unit(instruction)  
begin  
if @address and http = Net::HTTP.new(@address, 80)  
http.start do |http|  
request = Net::HTTP::Get.new(instruction)  
request.basic\_auth USERNAME, PASSWORD  
response = http.request(request)  
find\_state(response.body)  
end  
end  
rescue  
#Connection failed  
return nil  
end  
end

This works fine when the unit is working, but if someone switches the  
unit off, it takes a long time (30-40 sec!) to time out.

Is there a way to specify a timeout period for the request? Net::HTTP  
has a read\_timeout attribute, but I can see how to use it, if this is  
the approach to take.

---

<div class="post-metadata">

**Author:** ![reggieb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3e96dc/32.png) [@reggieb](https://www.ruby-forum.com/u/reggieb)\
**Post date:** [November 28, 2007, 2:40pm UTC](https://www.ruby-forum.com/t/net-http-reponse-method-custom-timeout/121492/2 "2007-11-28T14:40:40Z")

</div>

On 28 Nov 2007, at 12:22, Rob N. wrote:

> Is there a way to specify a timeout period for the request? Net::HTTP  
> has a read\_timeout attribute, but I can see how to use it, if this is  
> the approach to take.

Here’s what I’ve got

http = Net::HTTP.new(uri.host, uri.port)  
http.open\_timeout = x  
http.read\_timeout = y  
response = http.start do |http|  
http.request\_get(path)  
end

---

<div class="post-metadata">

**Author:** ![reggieb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3e96dc/32.png) [@reggieb](https://www.ruby-forum.com/u/reggieb)\
**Post date:** [November 28, 2007, 3:02pm UTC](https://www.ruby-forum.com/t/net-http-reponse-method-custom-timeout/121492/3 "2007-11-28T15:02:13Z")

</div>

Thank you Frederick,

This now works:

def send\_to\_unit(instruction)  
begin  
if @address and http = Net::HTTP.new(@address, 80)  
http.open\_timeout = 3  
http.read\_timeout = 3  
http.start do |http|  
request = Net::HTTP::Get.new(instruction)  
request.basic\_auth USERNAME, PASSWORD  
response = http.request(request)  
find\_state(response.body)  
end  
end  
rescue Timeout::Error  
#Connection failed  
return “not available - connection timed out”  
end  
end

Annoyingly - I looked at this post before I posted and the answer was  
all there if I’d look at it more closely:

[http://www.ruby-forum.com/topic/128439](http://www.ruby-forum.com/topic/128439)

So thank you for pointing me in the right direction. Splendid!
