【问题标题】:making specific request with httr用 httr 提出特定要求
【发布时间】:2016-03-12 02:33:02
【问题描述】:

如何使用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 参数以及接下来的内容。

【问题讨论】:

  • 你应该从你的问题中删除你的令牌。
  • 自然是假令牌

标签: r curl httr


【解决方案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)

【讨论】:

  • 谢谢。如果我想了解更多关于如何更好地将我的 curl 请求构建到 httr 中,你会推荐我去哪里?我不知道data参数的内容可以放在body中
  • 另一个小问题。如果您有一个变量而不是文件夹字符串,您将如何在其中连接它?我在使用 paste() 时遇到了一些麻烦......
  • httr 有一些小插曲 cran.rstudio.com/web/packages/httr - 我已经使用 httr ropensci.org/blog/2014/12/18/curl-options 写过关于各种卷曲选项的博客 - 对于可变问题,我不确定你在问什么
  • 非常感谢您的博客!对不起,没有澄清。我正在遍历文件夹名称,所以我想在 body 参数中插入一个变量而不是字符串 'folder'。不过,我在 httr 函数中使用 paste 时遇到了麻烦。你将如何在里面连接一个字符串和一个变量?
  • 哦,我明白了。我用 paste() 包围了参数。有道理为什么它不起作用。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-11
  • 2020-09-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多