httr2 (pronounced “hitter2”) is a comprehensive HTTP client that
provides a modern, pipeable API for working with web APIs. It builds on
top of {curl} to provide features
like explicit request objects, built-in rate limiting & retry tooling,
comprehensive OAuth support, and secure handling of secrets and
credentials.
You can now create and modify a request without performing it. This
means that there’s now a single function to perform the request and
fetch the result: req_perform(). req_perform() replaces
httr::GET(), httr::POST(), httr::DELETE(), and more.
HTTP errors are automatically converted into R errors. Use
req_error() to override the defaults (which turn all 4xx and 5xx
responses into errors) or to add additional details to the error
message.
You can automatically retry if the request fails or encounters a
transient HTTP error (e.g. a 429 rate limit request). req_retry()
defines the maximum number of retries, which errors are transient, and
how long to wait between tries.
OAuth support has been totally overhauled to directly support many
more flows and to make it much easier to both customise the built-in
flows and to create your own.
You can manage secrets (often needed for testing) with
secret_encrypt() and friends. You can obfuscate mildly confidential
data with obfuscate(), preventing it from being scraped from
published code.
You can automatically cache all cacheable results with req_cache().
Relatively few API responses are cacheable, but when they are it
typically makes a big difference.
Acknowledgements
httr2 wouldn’t be possible without curl,
openssl,
jsonlite, and
jose, which are all maintained by
Jeroen Ooms. A big thanks also go to Jenny
Bryan and Craig
Citro who have given me much useful
feedback on both the design of the internals and the user facing API.
httr2
httr2 (pronounced “hitter2”) is a comprehensive HTTP client that provides a modern, pipeable API for working with web APIs. It builds on top of {curl} to provide features like explicit request objects, built-in rate limiting & retry tooling, comprehensive OAuth support, and secure handling of secrets and credentials.
Installation
You can install httr2 from CRAN with:
Usage
To use httr2, start by creating a request:
You can tailor this request with the
req_family of functions:And see exactly what httr2 will send to the server with
req_dry_run():Use
req_perform()to perform the request, retrieving a response:The
resp_functions help you extract various useful components of the response:Major differences to httr
You can now create and modify a request without performing it. This means that there’s now a single function to perform the request and fetch the result:
req_perform().req_perform()replaceshttr::GET(),httr::POST(),httr::DELETE(), and more.HTTP errors are automatically converted into R errors. Use
req_error()to override the defaults (which turn all 4xx and 5xx responses into errors) or to add additional details to the error message.You can automatically retry if the request fails or encounters a transient HTTP error (e.g. a 429 rate limit request).
req_retry()defines the maximum number of retries, which errors are transient, and how long to wait between tries.OAuth support has been totally overhauled to directly support many more flows and to make it much easier to both customise the built-in flows and to create your own.
You can manage secrets (often needed for testing) with
secret_encrypt()and friends. You can obfuscate mildly confidential data withobfuscate(), preventing it from being scraped from published code.You can automatically cache all cacheable results with
req_cache(). Relatively few API responses are cacheable, but when they are it typically makes a big difference.Acknowledgements
httr2 wouldn’t be possible without curl, openssl, jsonlite, and jose, which are all maintained by Jeroen Ooms. A big thanks also go to Jenny Bryan and Craig Citro who have given me much useful feedback on both the design of the internals and the user facing API.