diff --git a/NEWS.md b/NEWS.md index f09f990e..e717f2c2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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. diff --git a/R/req-headers.R b/R/req-headers.R index e9435873..7e0998d6 100644 --- a/R/req-headers.R +++ b/R/req-headers.R @@ -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 @@ -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) @@ -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)) +} diff --git a/tests/testthat/test-req-headers.R b/tests/testthat/test-req-headers.R index 036a6790..1b7f3883 100644 --- a/tests/testthat/test-req-headers.R +++ b/tests/testthat/test-req-headers.R @@ -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) })