Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement req_headers_redacted() #608

Merged
merged 5 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading