Skip to content

Commit 860093c

Browse files
committed
Implement resp_raw
Roughly reconstructs the HTTP message that curl would've recieved
1 parent 8f02369 commit 860093c

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export(resp_header)
7777
export(resp_header_exists)
7878
export(resp_headers)
7979
export(resp_is_error)
80+
export(resp_raw)
8081
export(resp_retry_after)
8182
export(resp_status)
8283
export(resp_status_desc)

R/resp.R

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,28 @@ print.httr2_response <- function(x,...) {
8080
invisible(x)
8181
}
8282

83+
#' Show the raw response
84+
#'
85+
#' The reconstruct the HTTP message that httr2 received from the server.
86+
#' It's unlikely to be exactly the same (because most servers compress at
87+
#' least the body, and HTTP/2 can also compress the headers), but it conveys
88+
#' the same information.
89+
#'
90+
#' @param resp A HTTP [response]
91+
#' @export
92+
#' @examples
93+
#' resp <- request("https://httpbin.org/json") %>% req_fetch()
94+
#' resp %>% resp_raw()
95+
resp_raw <- function(resp) {
96+
cli::cat_line("HTTP/1.1 ", resp$status_code, " ", resp_status_desc(resp))
97+
cli::cat_line(names(resp$headers), ": ", resp$headers)
98+
cli::cat_line()
99+
if (!is.null(resp$body)) {
100+
cli::cat_line(resp_body_string(resp))
101+
}
102+
103+
}
104+
83105
is_response <- function(x) {
84106
inherits(x, "httr2_response")
85107
}

man/resp_raw.Rd

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)