-
Notifications
You must be signed in to change notification settings - Fork 4
/
parse_query.R
32 lines (30 loc) · 1.01 KB
/
parse_query.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#' Parse query string
#'
#' Parse http parameters from a query string. This includes unescaping
#' of url-encoded values.
#'
#' For http GET requests, the query string is specified
#' in the URL after the question mark. For http POST or PUT requests, the query
#' string can be used in the request body when the `Content-Type` header
#' is set to `application/x-www-form-urlencoded`.
#'
#' @export
#' @param query a url-encoded query string
#' @examples q <- "foo=1%2B1%3D2&bar=yin%26yang"
#' parse_query(q)
parse_query <- function(query){
if(is.raw(query))
query <- rawToChar(query);
stopifnot(is.character(query));
#httpuv includes the question mark in query string
query <- sub("^[?]", "", query)
query <- chartr('+',' ', query)
#split by & character
argstr <- strsplit(query, "&", fixed = TRUE)[[1]]
args <- lapply(argstr, function(x){
curl::curl_unescape(strsplit(x, "=", fixed = TRUE)[[1]])
})
values <- lapply(args, `[`, 2)
names(values) <- vapply(args, `[`, character(1), 1)
return(values)
}