Jagge
Jagge

Reputation: 978

specify timeout time in a POST or GET request with httr using r

I am running several scripts to post and retrieve data using the httr package. Occasionally, one of the jobs will just stall and never complete. This causes problems when other scripts assume that this script has completed. The stalling is due to some error on the server on the other end.

Is it, therefore, possible to specify a maximal waiting time for a POST/GET request?

I have looked at the documentation and found the httr::timeout() function. It passes arguments to curl::setopt(), and supposedly answers my question.

However, when I look at the curl option for timeout I find:

> curl::curl_options("^timeout$")
> timeout
> 13

And this is not what I am experiencing in practice? What Am I missing?

Upvotes: 5

Views: 2599

Answers (1)

symbolrush
symbolrush

Reputation: 7467

One option is to include the timeout specification directly in your GET request:

library(httr)
GET("http://httpbin.org/delay/3", timeout(1))

This returns a timeout error as follows:

Error in curl::curl_fetch_memory(url, handle = handle) : Timeout was reached: [httpbin.org] Operation timed out after 1000 milliseconds with 0 bytes received

Upvotes: 5

Related Questions