编辑:Margusl的答案非常有效!非常感谢Margus,我没有意识到HTtr已经被httr2取代了,因为我似乎一直在遵循过时的教程.感谢您抽出时间解释!!

我试图在R中调用REST API,但遇到了参数问题.

该API来自Todoist,一个待办事项列表/任务管理器(documentation here).我的目标是收集一定数量的已完成任务用于文档编制.

我能够连接到API并检索数据.

问题是,默认情况下,API返回30个已完成的任务.在cmd提示符中使用cURL,可以在末尾添加\ -d limit=200个任务,将限制更改为200个任务,我确实能够做到这一点.例如,cmd中的以下代码生成200个任务的预期结果:curl https://api.todoist.com/sync/v9/completed/get_all \ -H "Authorization: Bearer 000api_key000" \ -d limit=200

然而,在R中,我想不出该怎么做.我已经try 了将Limit=200与add_header()paste()相加的多种组合,但都没有成功.从根本上说,我遗漏了一些关于如何将curl-d参数转换为R中可接受的参数的内容.

下面是我的代码,它成功地完成了30个任务(删除了我的API密钥).任何关于如何如上所述传递API的限制参数的建议都将不胜感激.

library(httr)
library(jsonlite)

raw.result <- GET(url = "https://api.todoist.com", 
                  path = "/sync/v9/completed/get_all", 
                  add_headers(Authorization=paste("Bearer 000api_key000")))

推荐答案

curl命令中有-d/--data时,它实际上是一个带有表单数据的POST调用(即它不能与httr::GET()一起工作)

httr的继承者httr2附带了curl_translate()工具,它试图为你翻译curl个命令,结果通常是有点原始,但应该会给你正确的方向:

httr2::curl_translate('curl https://api.todoist.com/sync/v9/completed/get_all \ -H "Authorization: Bearer 000api_key000" \ -d limit=200')
#> request("https://api.todoist.com/sync/v9/completed/get_all") %>% 
#>   req_headers(
#>     Authorization = "Bearer 000api_key000",
#>   ) %>% 
#>   req_body_raw("limit=200", "application/x-www-form-urlencoded") %>% 
#>   req_perform()

要判断将发送到服务器的内容,我们可以将req_perform()临时替换为req_dry_run():

library(httr2)
request("https://api.todoist.com/sync/v9/completed/get_all") |>
  req_headers(
    Authorization = "Bearer 000api_key000",
  ) |>
  req_body_raw("limit=200", "application/x-www-form-urlencoded") |>
  req_dry_run()
#> POST /sync/v9/completed/get_all HTTP/1.1
#> Host: api.todoist.com
#> User-Agent: httr2/0.2.3 r-curl/5.1.0 libcurl/8.3.0
#> Accept: */*
#> Accept-Encoding: deflate, gzip
#> Authorization: <REDACTED>
#> Content-Type: application/x-www-form-urlencoded
#> Content-Length: 9
#> 
#> limit=200

由于有适当的方法用于承载令牌和表单正文,因此可以将相同的请求写为:

request("https://api.todoist.com/sync/v9/completed/get_all") |>
  req_auth_bearer_token("000api_key000") |>
  req_body_form(limit = 200) |>
  req_dry_run()
#> POST /sync/v9/completed/get_all HTTP/1.1
#> Host: api.todoist.com
#> User-Agent: httr2/0.2.3 r-curl/5.1.0 libcurl/8.3.0
#> Accept: */*
#> Accept-Encoding: deflate, gzip
#> Authorization: <REDACTED>
#> Content-Type: application/x-www-form-urlencoded
#> Content-Length: 9
#> 
#> limit=200

如果你需要帮助翻译成httr,也有https://curlconverter.com/r/,这将返回:

require(httr)

headers = c(
  `Authorization` = "Bearer 000api_key000",
  `Content-Type` = "application/x-www-form-urlencoded"
)

data = list(
  `limit` = "200"
)

res <- httr::POST(url = "https://api.todoist.com/sync/v9/completed/get_all", httr::add_headers(.headers=headers), body = data, encode = "form")

R相关问答推荐

是否有任何解决方案可以优化VSCode中RScript的图形绘制?

带有gplot 2的十字舱口

根据R中的另一个日期从多列中 Select 最近的日期和相应的结果

抖动点与嵌套类别变量箱形图的位置不对齐

在发布到PowerBI Service时,是否可以使用R脚本作为PowerBI的数据源?

使用gcuminc,如何使用逗号格式化风险表?

如何在所有绘图中保持条件值的 colored颜色 相同?

用关联字符串替换列名的元素

如何得到每四个元素向量R?

从所有项的 struct 相同的两级列表中,将该第二级中的所有同名项绑定在一起

我正在努力用R计算数据集中的中值逐步距离

当我添加美学时,geom_point未对齐

QY数据的处理:如何定义QY因素的水平

我如何go 掉盒子图底部的数字?

R -基线图-图形周围的阴影区域

名字的模糊匹配

如何合并不同列表中的数据文件,包括基于名称的部分匹配,而不是一对一等价

Ggplot2:添加更多特定 colored颜色 的线条

根据用户输入更改标记大小和 colored颜色 (R)

Gggvenn为Venn增加了不存在的价值