From 63b6e14e0fdf4f9c5ed01e2abf98a3c3ced12fde Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Mon, 6 Jan 2025 07:44:13 -0600 Subject: [PATCH] Implement `resp_request()` (#615) Fixes #604 --- NEWS.md | 1 + R/resp-request.R | 16 ++++++++++++++++ tests/testthat/test-resp-request.R | 5 +++++ 3 files changed, 22 insertions(+) create mode 100644 R/resp-request.R create mode 100644 tests/testthat/test-resp-request.R diff --git a/NEWS.md b/NEWS.md index b75ae880..20441a6d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # httr2 (development version) +* New `resp_request()` aids debugging by returning the request associated with a response (#604). * `print.request()` now correctly escapes `{}` in headers (#586). * 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). diff --git a/R/resp-request.R b/R/resp-request.R new file mode 100644 index 00000000..91aa5779 --- /dev/null +++ b/R/resp-request.R @@ -0,0 +1,16 @@ +#' Find the request responsible for a response +#' +#' To make debugging easier, httr2 includes the request that was used to +#' generate every response. You can use this function to access it. +#' +#' @inheritParams resp_header +#' @export +#' @examples +#' req <- request_test() +#' resp <- req_perform(req) +#' resp_request(resp) +resp_request <- function(resp) { + check_response(resp) + + resp$request +} diff --git a/tests/testthat/test-resp-request.R b/tests/testthat/test-resp-request.R new file mode 100644 index 00000000..e1f26447 --- /dev/null +++ b/tests/testthat/test-resp-request.R @@ -0,0 +1,5 @@ +test_that("can extract request", { + req <- request_test() + resp <- req_perform(req) + expect_equal(resp_request(resp), req) +})