Safely retry a request until it succeeds (returns an HTTP status code below 400). It is designed to be kind to the server: after each failure randomly waits up to twice as long. (Technically it uses exponential backoff with jitter, using the approach outlined in https://www.awsarchitectureblog.com/2015/03/backoff.html.)
RETRY(verb, url = NULL, config = list(), ..., body = NULL,
encode = c("multipart", "form", "json", "raw"), times = 3,
pause_base = 1, pause_cap = 60, handle = NULL, quiet = FALSE)
Name of verb to use.
the url of the page to retrieve
Additional configuration settings such as http
authentication (authenticate
), additional headers
(add_headers
), cookies (set_cookies
) etc.
See config
for full details and list of helpers.
Further named parameters, such as query
, path
, etc,
passed on to modify_url
. Unnamed parameters will be combined
with config
.
One of the following:
FALSE
: No body. This is typically not used with POST
,
PUT
, or PATCH
, but can be useful if you need to send a
bodyless request (like GET
) with VERB()
.
NULL
: An empty body
""
: A length 0 body
upload_file("path/")
: The contents of a file. The mime
type will be guessed from the extension, or can be supplied explicitly
as the second argument to upload_file()
A character or raw vector: sent as is in body. Use
content_type
to tell the server what sort of data
you are sending.
A named list: See details for encode.
If the body is a named list, how should it be encoded? Can be one of form (application/x-www-form-urlencoded), multipart, (multipart/form-data), or json (application/json).
For "multipart", list elements can be strings or objects created by
upload_file
. For "form", elements are coerced to strings
and escaped, use I()
to prevent double-escaping. For "json",
parameters are automatically "unboxed" (i.e. length 1 vectors are
converted to scalars). To preserve a length 1 vector as a vector,
wrap in I()
. For "raw", either a character or raw vector. You'll
need to make sure to set the content_type()
yourself.
Maximum number of requests to attempt.
This method uses exponential back-off with
full jitter - this means that each request will randomly wait between 0
and pause_base * 2 ^ attempt
seconds, up to a maximum of
pause_cap
seconds.
The handle to use with this request. If not
supplied, will be retrieved and reused from the handle_pool
based on the scheme, hostname and port of the url. By default httr
requests to the same scheme/host/port combo. This substantially reduces
connection time, and ensures that cookies are maintained over multiple
requests to the same host. See handle_pool
for more
details.
If FALSE
, will print a message displaying how long
until the next request.
The last response. Note that if the request doesn't succeed after
times
times this will be a failed request, i.e. you still need
to use stop_for_status()
.
# NOT RUN {
# Succeeds straight away
RETRY("GET", "http://httpbin.org/status/200")
# Never succeeds
RETRY("GET", "http://httpbin.org/status/500")
# }
Run the code above in your browser using DataLab