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

Control how spaces are handed in query strings #609

Merged
merged 6 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# httr2 (development version)

* `req_url_query()` gains the ability to control how spaces are encoded (#432).
* `url_parse()` now uses `curl::curl_parse_url()` which is much faster and more correct (#577).
* `req_retry()` now defaults to `max_tries = 2` with a message.
Set to `max_tries = 1` to disable retries.
Expand Down
9 changes: 7 additions & 2 deletions R/req-url.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,17 @@ req_url <- function(req, url) {
#' If none of these options work for your needs, you can instead supply a
#' function that takes a character vector of argument values and returns a
#' a single string.
#' @param .space How should spaces in query params be escaped? The default,
#' "percent", uses standard percent encoding (i.e. `%20%`), but you can opt-in
hadley marked this conversation as resolved.
Show resolved Hide resolved
#' to "form" encoding, which uses `+` instead.
req_url_query <- function(.req,
...,
.multi = c("error", "comma", "pipe", "explode")) {
.multi = c("error", "comma", "pipe", "explode"),
.space = c("percent", "form")
) {
check_request(.req)

dots <- multi_dots(..., .multi = .multi)
dots <- multi_dots(..., .multi = .multi, .space = .space)

url <- url_parse(.req$url)
url$query <- modify_list(url$query, !!!dots)
Expand Down
7 changes: 6 additions & 1 deletion R/url.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,19 @@ elements_build <- function(x, name, collapse, error_call = caller_env()) {
format_query_param <- function(x,
name,
multi = FALSE,
form = FALSE,
error_call = caller_env()) {
check_query_param(x, name, multi = multi, error_call = error_call)

if (inherits(x, "AsIs")) {
unclass(x)
} else {
x <- format(x, scientific = FALSE, trim = TRUE, justify = "none")
curl::curl_escape(x)
x <- curl::curl_escape(x)
if (form) {
x <- gsub("%20", "+", x, fixed = TRUE)
}
x
}
}
check_query_param <- function(x, name, multi = FALSE, error_call = caller_env()) {
Expand Down
18 changes: 13 additions & 5 deletions R/utils-multi.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
multi_dots <- function(...,
.multi = c("error", "comma", "pipe", "explode"),
.space = c("percent", "form"),
error_arg = "...",
error_call = caller_env()) {
if (is.function(.multi)) {
check_function2(.multi, call = error_call, arg = ".multi")
} else {
.multi <- arg_match(.multi, error_arg = ".multi", error_call = error_call)
}
.space <- arg_match(.space, call = error_call)
form <- .space == "form"

dots <- list2(...)
if (length(dots) == 0) {
Expand All @@ -31,20 +34,20 @@ multi_dots <- function(...,
n <- lengths(dots)
if (any(n > 1)) {
if (is.function(.multi)) {
dots[n > 1] <- imap(dots[n > 1], format_query_param, multi = TRUE)
dots[n > 1] <- imap(dots[n > 1], format_query_param, multi = TRUE, form = form)
dots[n > 1] <- lapply(dots[n > 1], .multi)
dots[n > 1] <- lapply(dots[n > 1], I)
} else if (.multi == "comma") {
dots[n > 1] <- imap(dots[n > 1], format_query_param, multi = TRUE)
dots[n > 1] <- imap(dots[n > 1], format_query_param, multi = TRUE, form = form)
dots[n > 1] <- lapply(dots[n > 1], paste0, collapse = ",")
dots[n > 1] <- lapply(dots[n > 1], I)
} else if (.multi == "pipe") {
dots[n > 1] <- imap(dots[n > 1], format_query_param, multi = TRUE)
dots[n > 1] <- imap(dots[n > 1], format_query_param, multi = TRUE, form = form)
dots[n > 1] <- lapply(dots[n > 1], paste0, collapse = "|")
dots[n > 1] <- lapply(dots[n > 1], I)
} else if (.multi == "explode") {
dots <- explode(dots)
dots[n > 1] <- imap(dots[n > 1], format_query_param, multi = TRUE)
dots[n > 1] <- imap(dots[n > 1], format_query_param, multi = TRUE, form = form)
dots[n > 1] <- lapply(dots[n > 1], I)
} else if (.multi == "error") {
cli::cli_abort(
Expand All @@ -58,7 +61,12 @@ multi_dots <- function(...,
}

# Format other params
dots[n == 1] <- imap(dots[n == 1], format_query_param, error_call = error_call)
dots[n == 1] <- imap(
dots[n == 1],
format_query_param,
form = form,
error_call = error_call
)
dots[n == 1] <- lapply(dots[n == 1], I)

dots
Expand Down
11 changes: 10 additions & 1 deletion man/req_url.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/req-url.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# can control space handling

Code
req_url_query(req, a = " ", .space = "bar")
Condition
Error in `multi_dots()`:
! `.space` must be one of "percent" or "form", not "bar".

# can handle multi query params

Code
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-req-url.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ test_that("can set query params", {
expect_equal(req_url_query(req, !!!list(a = 1, a = 2))$url, "http://example.com/?a=1&a=2")
})

test_that("can control space handling", {
req <- request("http://example.com/")
expect_equal(req_url_query(req, a = " ")$url, "http://example.com/?a=%20")
expect_equal(req_url_query(req, a = " ", .space = "form")$url, "http://example.com/?a=+")

expect_snapshot(
req_url_query(req, a = " ", .space = "bar"),
error = TRUE
)
})

test_that("can handle multi query params", {
req <- request("http://example.com/")

Expand Down
Loading