Skip to content

Commit e8fea4e

Browse files
authored
Export req_get_method() and req_get_body() (#744)
Formerly known as `req_method_get()` and `req_body_get()`, but I think it's better to put exported accessors behind a common prefix. Fixes #718
1 parent e0c71f7 commit e8fea4e

File tree

14 files changed

+100
-24
lines changed

14 files changed

+100
-24
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ export(req_cookie_preserve)
7474
export(req_cookies_set)
7575
export(req_dry_run)
7676
export(req_error)
77+
export(req_get_body)
78+
export(req_get_method)
7779
export(req_headers)
7880
export(req_headers_redacted)
7981
export(req_method)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# httr2 (development version)
22

3+
* New `req_get_method()` and `req_get_body()` allow you to do some limited prediction of what a request _will_ do when it's performed (#718).
34
* Functions that capture interrutps (like `req_perform_parallel()` and friends) are now easier to escape if they're called inside a loop: you can press Ctrl + C twice to guarantee an exit (#1810).
45
* New `last_request_json()` and `last_response_json()` to conveniently see JSON bodies (#734).
56
* `req_body_json_modify()` can now be used on a request with an empty body.

R/last.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ last_request_json <- function(pretty = TRUE) {
5050
if (req_body_type(req) != "json") {
5151
cli::cli_abort("Last request doesn't have a JSON body.")
5252
}
53-
httr2_json(req_body_get(req), pretty = pretty)
53+
httr2_json(req_get_body(req), pretty = pretty)
5454
}
5555

5656
#' @param pretty Should the JSON be pretty-printed?

R/req-auth-aws.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ auth_aws_sign <- function(
7171
) {
7272
current_time <- Sys.time()
7373

74-
body_sha256 <- openssl::sha256(req_body_get(req))
74+
body_sha256 <- openssl::sha256(req_get_body(req))
7575

7676
# We begin by adding some necessary headers that must be added before
7777
# canoncalization even thought they aren't documented until later
@@ -83,7 +83,7 @@ auth_aws_sign <- function(
8383
)
8484

8585
signature <- aws_v4_signature(
86-
method = req_method_get(req),
86+
method = req_get_method(req),
8787
url = url_parse(req$url),
8888
headers = req$headers,
8989
body_sha256 = body_sha256,

R/req-body.R

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,23 @@ req_body_info <- function(req) {
217217
multipart = "multipart data"
218218
)
219219
}
220-
req_body_get <- function(req) {
220+
221+
222+
#' Retrieve the body that a request will use
223+
#'
224+
#' Mimics the algorithm that curl uses to determine the body.
225+
#'
226+
#' @inheritParams req_perform
227+
#' @keywords internal
228+
#' @export
229+
#' @examples
230+
#' req <- request(example_url())
231+
#' req |> req_body_raw("abc") |> req_get_body()
232+
#' req |> req_body_file(system.file("DESCRIPTION")) |> req_get_body()
233+
#' req |> req_body_json(list(x = 1, y = 2)) |> req_get_body()
234+
#' req |> req_body_form(x = 1, y = 2) |> req_get_body()
235+
#' req |> req_body_multipart(x = "x", y = "y") |> req_get_body() |> cat()
236+
req_get_body <- function(req) {
221237
switch(
222238
req_body_type(req),
223239
empty = NULL,
@@ -244,8 +260,8 @@ req_body_apply <- function(req) {
244260
raw = req_body_apply_raw(req, req$body$data),
245261
string = req_body_apply_string(req, req$body$data),
246262
file = req_body_apply_connection(req, req$body$data),
247-
json = req_body_apply_string(req, req_body_get(req)),
248-
form = req_body_apply_string(req, req_body_get(req)),
263+
json = req_body_apply_string(req, req_get_body(req)),
264+
form = req_body_apply_string(req, req_get_body(req)),
249265
multipart = req_body_apply_multipart(req, req$body$data),
250266
)
251267

R/req-cache.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ cache_pre_fetch <- function(req, path = NULL) {
209209

210210
# Only GET requests should be retrieved from cache. It's not sufficient to
211211
# only save GET requests, because the method is not part of the cache key
212-
if (req_method_get(req) != "GET") {
212+
if (req_get_method(req) != "GET") {
213213
return(req)
214214
}
215215

R/req-method.R

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,21 @@ req_method_apply <- function(req) {
3333
)
3434
}
3535

36-
# Guess the method that curl will used based on options
37-
# https://everything.curl.dev/libcurl-http/requests#request-method
38-
req_method_get <- function(req) {
36+
#' Retrieve the method that a request will use
37+
#'
38+
#' Mimics the algorithm that curl uses to determine the method.
39+
#'
40+
#' @inheritParams req_perform
41+
#' @export
42+
#' @keywords internal
43+
#' @examples
44+
#' req <- request(example_url())
45+
#' req_get_method(req)
46+
#' req_get_method(req |> req_body_raw("abc"))
47+
#' req_get_method(req |> req_method("DELETE"))
48+
#' req_get_method(req |> req_method("HEAD"))
49+
req_get_method <- function(req) {
50+
# https://everything.curl.dev/libcurl-http/requests#request-method
3951
if (!is.null(req$method)) {
4052
req$method
4153
} else if (has_name(req$options, "nobody")) {

R/req.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ request <- function(base_url) {
2424
#' @export
2525
print.httr2_request <- function(x, ..., redact_headers = TRUE) {
2626
cli::cli_text("{.cls {class(x)}}")
27-
method <- toupper(req_method_get(x))
27+
method <- toupper(req_get_method(x))
2828
cli::cli_text("{.strong {method}} {x$url}")
2929

3030
bullets_with_header(

man/req_get_body.Rd

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

man/req_get_method.Rd

Lines changed: 22 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)