如何将数组传递到http的查询中?请求url应该是这样的:https://www.example.com/xyz?type=3& type = 5
我当前的代码是这样的:
POST(url,
query = data.frame("something" = "somethingElse", type = ),
add_headers(.headers = c("token" = token),
encode = "json")
我如何将这些类型从url示例添加到我的R示例?
httr
的默认编码不喜欢多次使用相同的名称,但是可以将您的值分离到具有重复名称的列表中。下面是我使用的一个帮助函数,它可以帮助
flattenbody <- function(x) {
# A form/query can only have one value per name, so take
# any values that contain vectors length >1 and
# split them up
# list(x=1:2, y="a") becomes list(x=1, x=2, y="a")
if (all(lengths(x)<=1)) return(x);
do.call("c", mapply(function(name, val) {
if (length(val)==1 || any(c("form_file", "form_data") %in% class(val))) {
x <- list(val)
names(x) <- name
x
} else {
x <- as.list(val)
names(x) <- rep(name, length(val))
x
}
}, names(x), x, USE.NAMES = FALSE, SIMPLIFY = FALSE))
}
可以和
一起使用POST(url,
query = flattenbody(list(something="somethingElse", type = c(3, 5))),
add_headers(.headers = c("token" = token)),
encode = "json"
)