Skip to content

Commit

Permalink
Implement req_headers_redacted()
Browse files Browse the repository at this point in the history
Which I think is a friendlier interface than the `.redact` argument to `req_headers()`. Fixes #561
  • Loading branch information
hadley committed Dec 23, 2024
1 parent 9db8f7e commit 3f34eb8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# httr2 (development version)

* New `req_headers_redacted()` provides a user-friendlier way to set redacted headers (#561).
* `url_parse()` now uses `curl::curl_parse_url()` which is much faster and more correct (#577).
* `req_retry()` now defaults to `max_tries = 2` with a message.
Set to `max_tries = 1` to disable retries.
Expand Down
17 changes: 15 additions & 2 deletions R/req-headers.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#' Modify request headers
#'
#' `req_headers()` allows you to set the value of any header.
#' `req_headers()` allows you to set the value of any header. Use
#' `req_headers_redacted()` to add "redacted" headers which httr2 strives
#' to avoid printing to the console. This is good practice for headers used
#' for authentication to avoid accidentally including them in log files.
#'
#' @param .req A [request].
#' @param ... <[`dynamic-dots`][rlang::dyn-dots]> Name-value pairs of headers
Expand Down Expand Up @@ -51,7 +54,8 @@
#'
#' # Use `.redact` to hide a header in the output
#' req |>
#' req_headers(Secret = "this-is-private", Public = "but-this-is-not", .redact = "Secret") |>
#' req_headers_redacted(Secret = "this-is-private") |>
#' req_headers(Public = "but-this-is-not") |>
#' req_dry_run()
req_headers <- function(.req, ..., .redact = NULL) {
check_request(.req)
Expand All @@ -68,3 +72,12 @@ req_headers <- function(.req, ..., .redact = NULL) {

.req
}

#' @export
#' @rdname req_headers
req_headers_redacted <- function(.req, ...) {
check_request(.req)

dots <- list(...)
req_headers(.req, !!!dots, .redact = names(dots))
}
2 changes: 2 additions & 0 deletions tests/testthat/test-req-headers.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ test_that("can control which headers to redact", {
expect_redact(req_headers(req, a = 1L, b = 2L, .redact = c("a", "b")), c("a", "b"))
expect_redact(req_headers(req, a = 1L, b = 2L, .redact = "a"), "a")

expect_redact(req_headers_redacted(req, a = 1L, b = 2L), c("a", "b"))

expect_snapshot(error = TRUE, {
req_headers(req, a = 1L, b = 2L, .redact = 1L)
})
Expand Down

0 comments on commit 3f34eb8

Please sign in to comment.