42

I am using RestClient gem by making get call to the server through it. The question is how do I set the timeout from client side.

RestClient.get "http://127.0.0.1:7819/tokenize/word/stackoverflow"

I want to set it to 10 seconds.

Thanks in Advance!!

sravan_kumar
  • 1,129
  • 1
  • 13
  • 25

1 Answers1

67

You don't need to monkey patch anything. You can use RestClient::Request directly, like:

RestClient::Request.execute(:method => :get, :url => url, :timeout => 10, :open_timeout => 10)

But remember the worst case scenario is 20 seconds.

Check the other post answer https://stackoverflow.com/a/5445421/565999

Community
  • 1
  • 1
Hugo Lopes Tavares
  • 28,528
  • 5
  • 47
  • 45
  • 2
    This should be the answer - I'm afraid that I must assert that monkey patches are evil due to the unintended side effects they have. – Asfand Qazi Aug 30 '12 at 16:11
  • 3
    nit: you don't have to specify both timeout and open_timeout if those two are same value. timeout value will be used for both read_timeout and open_timeout. https://github.com/rest-client/rest-client/blob/10d3599745a134fa69eccc2f16f6e70133a3bb6c/lib/restclient/request.rb#L130 – Kazuki Nov 13 '15 at 05:24
  • When using **RestClient::Resource**, could try `RestClient::Resource.new('http://slow', :timeout => 10)` – Jing Li Jun 24 '16 at 10:49