Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Make HTTP requests and process their responses. A modern reimagining of httr.

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md
NotificationsYou must be signed in to change notification settings

r-lib/httr2

Repository files navigation

R-CMD-checkCodecov test coverage

httr2 (pronounced “hitter2”) is a comprehensive HTTP client thatprovides a modern, pipeable API for working with web APIs. It builds ontop of{curl} to provide featureslike explicit request objects, built-in rate limiting & retry tooling,comprehensive OAuth support, and secure handling of secrets andcredentials.

Installation

You can install httr2 from CRAN with:

install.packages("httr2")

Usage

To use httr2, start by creating arequest:

library(httr2)req<- request("https://r-project.org")req#> <httr2_request>#> GET https://r-project.org#> Body: empty

You can tailor this request with thereq_ family of functions:

# Add custom headersreq|> req_headers("Accept"="application/json")#> <httr2_request>#> GET https://r-project.org#> Headers:#> * Accept: "application/json"#> Body: empty# Add a body, turning it into a POSTreq|> req_body_json(list(x=1,y=2))#> <httr2_request>#> POST https://r-project.org#> Body: JSON data# Modify the path in the urlreq|> req_url_path(path="path/to/my/file")#> <httr2_request>#> GET https://r-project.org/path/to/my/file#> Body: empty# Automatically retry if the request failsreq|> req_retry(max_tries=5)#> <httr2_request>#> GET https://r-project.org#> Body: empty#> Policies:#> * retry_max_tries        : 5#> * retry_on_failure       : FALSE#> * retry_failure_threshold: Inf#> * retry_failure_timeout  : 30#> * retry_realm            : "r-project.org"# Change the HTTP methodreq|> req_method("PATCH")#> <httr2_request>#> PATCH https://r-project.org#> Body: empty

And see exactly what httr2 will send to the server withreq_dry_run():

req|> req_dry_run()#> GET / HTTP/1.1#> accept: */*#> accept-encoding: deflate, gzip#> host: r-project.org#> user-agent: httr2/1.2.1.9000 r-curl/7.0.0 libcurl/8.14.1

Usereq_perform() to perform the request, retrieving aresponse:

resp<- req_perform(req)resp#> <httr2_response>#> GET https://www.r-project.org/#> Status: 200 OK#> Content-Type: text/html#> Body: In memory (6238 bytes)

Theresp_ functions help you extract various useful components of theresponse:

resp|> resp_content_type()#> [1] "text/html"resp|> resp_status_desc()#> [1] "OK"resp|> resp_body_html()#> {html_document}#> <html lang="en">#> [1] <head>\n<meta http-equiv="Content-Type" content="text/html; charset=UTF-8 ...#> [2] <body>\n    <div>\n      <div>\n       ...

Major differences to httr

  • You can now create and modify a request without performing it. Thismeans that there’s now a single function to perform the request andfetch the result:req_perform().req_perform() replaceshttr::GET(),httr::POST(),httr::DELETE(), and more.

  • HTTP errors are automatically converted into R errors. Usereq_error() to override the defaults (which turn all 4xx and 5xxresponses into errors) or to add additional details to the errormessage.

  • You can automatically retry if the request fails or encounters atransient HTTP error (e.g. a 429 rate limit request).req_retry()defines the maximum number of retries, which errors are transient, andhow long to wait between tries.

  • OAuth support has been totally overhauled to directly support manymore flows and to make it much easier to both customise the built-inflows and to create your own.

  • You can manage secrets (often needed for testing) withsecret_encrypt() and friends. You can obfuscate mildly confidentialdata withobfuscate(), preventing it from being scraped frompublished code.

  • You can automatically cache all cacheable results withreq_cache().Relatively few API responses are cacheable, but when they are ittypically makes a big difference.

Acknowledgements

httr2 wouldn’t be possible withoutcurl,openssl,jsonlite, andjose, which are all maintained byJeroen Ooms. A big thanks also go toJennyBryan andCraigCitro who have given me much usefulfeedback on both the design of the internals and the user facing API.

About

Make HTTP requests and process their responses. A modern reimagining of httr.

Topics

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Contributors60

Languages


[8]ページ先頭

©2009-2025 Movatter.jp