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

Combining items in parse_query when they share the same name? #4

Open
meztez opened this issue Apr 29, 2020 · 0 comments
Open

Combining items in parse_query when they share the same name? #4

meztez opened this issue Apr 29, 2020 · 0 comments

Comments

@meztez
Copy link
Contributor

meztez commented Apr 29, 2020

parse_query("?a=1&a=2")
# $a
# [1] "1"
# 
# $a
# [1] "2"

Per RFC 6570 6.2.1, should parse_body return something like?

# $a
# [1] "1" "2"

Something like that could do it, could PR if you think it makes sense. Thank you for your consideration.

parse_query <- function (query) 
{
    if (is.raw(query)) 
        query <- rawToChar(query)
    stopifnot(is.character(query))
    query <- sub("^[?]", "", query)
    query <- chartr("+", " ", query)
    argstr <- strsplit(query, "&", fixed = TRUE)[[1]]
    args <- lapply(argstr, function(x) {
        curl::curl_unescape(strsplit(x, "=", fixed = TRUE)[[1]])
    })
    values <- lapply(args, `[`, 2)
    keys <- vapply(args, `[`, character(1), 1)
    values <- sapply(unique(keys), function(x) do.call(c, values[x == keys]), simplify = FALSE)
    return(values)
}

parse_query("?a=1&a=2")
# $a
# [1] "1" "2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant