req_perform {httr2} | R Documentation |
Perform a request to get a response
Description
After preparing a request, call req_perform()
to perform it, fetching
the results back to R as a response.
The default HTTP method is GET
unless a body (set by req_body_json and
friends) is present, in which case it will be POST
. You can override
these defaults with req_method()
.
Usage
req_perform(
req,
path = NULL,
verbosity = NULL,
mock = getOption("httr2_mock", NULL),
error_call = current_env()
)
Arguments
req |
A request. |
path |
Optionally, path to save body of the response. This is useful for large responses since it avoids storing the response in memory. |
verbosity |
How much information to print? This is a wrapper
around
Use |
mock |
A mocking function. If supplied, this function is called
with the request. It should return either |
error_call |
The execution environment of a currently
running function, e.g. |
Value
If the HTTP request succeeds, and the status code is ok (e.g. 200), an HTTP response.
If the HTTP request succeeds, but the status code is an error (e.g a 404), an error with class
c("httr2_http_404", "httr2_http")
. By default, all 400 and 500 status codes will be treated as an error, but you can customise this withreq_error()
.If the HTTP request fails (e.g. the connection is dropped or the server doesn't exist), an error with class
"httr2_failure"
.
Requests
Note that one call to req_perform()
may perform multiple HTTP requests:
If the
url
is redirected with a 301, 302, 303, or 307, curl will automatically follow theLocation
header to the new location.If you have configured retries with
req_retry()
and the request fails with a transient problem,req_perform()
will try again after waiting a bit. Seereq_retry()
for details.If you are using OAuth, and the cached token has expired,
req_perform()
will get a new token either using the refresh token (if available) or by running the OAuth flow.
Progress bar
req_perform()
will automatically add a progress bar if it needs to wait
between requests for req_throttle()
or req_retry()
. You can turn the
progress bar off (and just show the total time to wait) by setting
options(httr2_progress = FALSE)
.
See Also
req_perform_parallel()
to perform multiple requests in parallel.
req_perform_iterative()
to perform multiple requests iteratively.
Examples
request("https://google.com") |>
req_perform()