Skip to content

Commit

Permalink
Implement req_headers_redacted() (#608)
Browse files Browse the repository at this point in the history
I think this is a friendlier interface than the `.redact` argument to `req_headers()`. Fixes #561
  • Loading branch information
hadley authored Jan 6, 2025
1 parent 0dbb703 commit 4a52f4f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 12 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export(req_cookies_set)
export(req_dry_run)
export(req_error)
export(req_headers)
export(req_headers_redacted)
export(req_method)
export(req_oauth)
export(req_oauth_auth_code)
Expand Down
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).
* `resp_link_url()` now works if there are multiple `Link` headers (#587).
* New `url_modify()` makes it easier to modify an existing url (#464).
* New `req_url_relative()` for constructing relative urls (#449).
Expand Down
29 changes: 23 additions & 6 deletions R/req-headers.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#' Modify request headers
#'
#' @description
#' `req_headers()` allows you to set the value of any header.
#'
#' `req_headers_redacted()` is a variation that adds "redacted" headers, which
#' httr2 avoids printing on the console. This is good practice for
#' authentication headers to avoid accidentally leaking them in log files.
#'
#' @param .req A [request].
#' @param ... <[`dynamic-dots`][rlang::dyn-dots]> Name-value pairs of headers
#' and their values.
Expand Down Expand Up @@ -46,13 +51,16 @@
#' # If you have headers in a list, use !!!
#' headers <- list(HeaderOne = "one", HeaderTwo = "two")
#' req |>
#' req_headers(!!!headers, HeaderThree = "three") |>
#' req_dry_run()
#'
#' # 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(!!!headers, HeaderThree = "three") |>
#' req_dry_run()
#'
#' # Use `req_headers_redacted()`` to hide a header in the output
#' req_secret <- req |>
#' req_headers_redacted(Secret = "this-is-private") |>
#' req_headers(Public = "but-this-is-not")
#'
#' req_secret
#' req_secret |> req_dry_run()
req_headers <- function(.req, ..., .redact = NULL) {
check_request(.req)

Expand All @@ -68,3 +76,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))
}
22 changes: 16 additions & 6 deletions man/req_headers.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 4a52f4f

Please sign in to comment.