用httr做出具体要求

时间:2016-03-12 02:33:03

标签: r curl httr

我如何使用httr发出此请求?

'curl -X POST https://api.dropboxapi.com/2/files/list_folder \
    --header "Authorization: Bearer 21318318usdhsdha9283718 " \
    --header "Content-Type: application/json" \
    --data "{\"path\": \"/today/\",\"recursive\": false,\"include_media_info\": false,\"include_deleted\": false}"'

我尝试过使用curlconverter,但对这个问题效果不佳。我不确定如何实现--data参数以及后面的内容。

1 个答案:

答案 0 :(得分:1)

这对我有用,对你有用吗?

httr::POST(
  "https://api.dropboxapi.com/2/files/list_folder",
  add_headers(Authorization = "Bearer <token>"),
  content_type_json(),
  body = "{\"path\": \"/folder\",\"recursive\": false,\"include_media_info\": false,\"include_deleted\": false}",
  encode = "json"
)

如果你想为许多文件夹概括一点:

library("httr")
foobar <- function(x) {
  content(POST(
    "https://api.dropboxapi.com/2/files/list_folder",
    add_headers(Authorization = "Bearer <token>"),
    content_type_json(),
    body = list(path = paste0("/", x), recursive = FALSE, 
                include_media_info = FALSE, include_deleted = FALSE),
    encode = "json"
  ))
}

lapply(c('a', 'b', "c"), foobar)