How to refresh or retry a specific web page using httr GET command?

Look at purrr::safely(). You can wrap GET as such:

safe_GET <- purrr::safely(GET)

This removes the ugliness of tryCatch() by letting you do:

resp <- safe_GET("http://example.com") # you can use all legal `GET` params

And you can test resp$result for NULL. Put that into your retry loop and you're good to go.

You can see this in action by doing:

str(safe_GET("https://httpbin.org/delay/3", timeout(1)))

which will ask the httpbin service to wait 3s before responding but set an explicit timeout on the GET request to 1s. I wrapped it in str() to show the result:

List of 2
 $ result: NULL
 $ error :List of 2
  ..$ message: chr "Timeout was reached"
  ..$ call   : language curl::curl_fetch_memory(url, handle = handle)
  ..- attr(*, "class")= chr [1:3] "simpleError" "error" "condition"

So, you can even check the message if you need to.


http_status(h1) can help you know where the problem lies :

a <- http_status(GET("http://google.com"))
a

$category
[1] "Success"

$reason
[1] "OK"

$message
[1] "Success: (200) OK"

and

b <- http_status(GET("http://google.com/blablablablaba"))
b

$category
[1] "Client error"

$reason
[1] "Not Found"

$message
[1] "Client error: (404) Not Found"

See this list of HTTP status codes to know what the code you get means.

Moreover, tryCatch can help you achieve what you want :

tryCatch({GET(h1)}, error = function(e){print("error")})

Tags:

Timeout

R

Get

Httr