Skip to content

Escaping ascii characters in queries #152

@boshek

Description

@boshek

👋 Thanks for your continued work on this. Love the "lazy" nature of httr2 v httr

When httr2 is passed a collapsed string of query parameters it automatically escapes then which seems sensible:

library(httr2)
req <- request("http://example.com")

# Change url components
two_queries <- c("one", "two")
collapsed_query <- paste0(two_queries, collapse = ",")

## automatically converts , to %2
(req <- req %>%
  req_url_query(q = collapsed_query))
#> <httr2_request>
#> GET http://example.com?q=one%2Ctwo
#> Body: empty

In {httr} there was, however, the option of unescaping that using I:

library(httr)
r <- GET("http://example.com",
  query = list(q = I(collapsed_query))
)

## can use I to use as is
r$url
#> [1] "http://example.com/?q=one,two"

This does not work in {httr2}. In {httr} this is accomplished via:
https://github.com/r-lib/httr/blob/21ff69f219ad11298854a63b8f753389088cf382/R/url-query.r#L25-L28

So unless I am missing something (which is of course possible!) we are stuck with doing stuff like:

req <- req %>%
  req_url_query(q = collapsed_query)
req$url <- curl::curl_unescape(req$url)
req
#> <httr2_request>
#> GET http://example.com?q=one,two
#> Body: empty

Would you be amenable to a PR that added an escape-like function somewhere, probably in httr2:::url_build`?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions