req_url()
replaces the entire urlreq_url_query()
modifies the components of the queryreq_url_path()
modifies the pathreq_url_path_append()
adds to the path
Usage
req_url(req, url)
req_url_query(.req, ..., .multi = c("error", "comma", "pipe", "explode"))
req_url_path(req, ...)
req_url_path_append(req, ...)
Arguments
- req, .req
A httr2 request object.
- url
New URL; completely replaces existing.
- ...
For
req_url_query()
: <dynamic-dots
> Name-value pairs that define query parameters. Each value must be either an atomic vector orNULL
(which removes the corresponding parameters). If you want to opt out of escaping, wrap strings inI()
.For
req_url_path()
andreq_url_path_append()
: A sequence of path components that will be combined with/
.- .multi
Controls what happens when an element of
...
is a vector containing multiple values:"error"
, the default, throws an error."comma"
, separates values with a,
, e.g.?x=1,2
."pipe"
, separates values with a|
, e.g.?x=1|2
."explode"
, turns each element into its own parameter, e.g.?x=1&x=2
.
If none of these functions work, you can alternatively supply a function that takes a character vector and returns a string.
Value
A modified HTTP request.
Examples
req <- request("http://example.com")
# Change url components
req |>
req_url_path_append("a") |>
req_url_path_append("b") |>
req_url_path_append("search.html") |>
req_url_query(q = "the cool ice")
#> <httr2_request>
#> GET http://example.com/a/b/search.html?q=the%20cool%20ice
#> Body: empty
# Change complete url
req |>
req_url("http://google.com")
#> <httr2_request>
#> GET http://google.com
#> Body: empty
# Use .multi to control what happens with vector parameters:
req |> req_url_query(id = 100:105, .multi = "comma")
#> <httr2_request>
#> GET http://example.com?id=100,101,102,103,104,105
#> Body: empty
req |> req_url_query(id = 100:105, .multi = "explode")
#> <httr2_request>
#> GET http://example.com?id=100&id=101&id=102&id=103&id=104&id=105
#> Body: empty
# If you have query parameters in a list, use !!!
params <- list(a = "1", b = "2")
req |>
req_url_query(!!!params, c = "3")
#> <httr2_request>
#> GET http://example.com?a=1&b=2&c=3
#> Body: empty