阅读 API - 从 Curl 到 R 的代码

Reading API - code from Curl to R

我正在尝试使用 R 从经过身份验证的 API 中读取 json,但未成功。

我有 Curl 代码并尝试使用 "curlconverter" 库将其转换为 R,还尝试使用 "httr" 库获取它。

curl -X GET \
  'https://api.cartolafc.globo.com/auth/liga/gurudocartola-com?orderBy=campeonato&page=1' \
  -H 'Cache-Control: no-cache' \
  -H 'x-glb-token: mytoken'

我希望能有一个用 R 编写这段代码的解决方案。

library(curlconverter) # devtools::install_github("hrbrmstr/curlconverter")

u <- "curl -X GET 'https://api.cartolafc.globo.com/auth/liga/gurudocartola-com?orderBy=campeonato&page=1' -H 'Cache-Control: no-cache' -H 'x-glb-token: mytoken'"

straighten(u) %>% 
  make_req()

这使得:

httr::VERB(verb = "GET", url = "https://api.cartolafc.globo.com/auth/liga/gurudocartola-com?orderBy=campeonato&page=1", 
           httr::add_headers(`Cache-Control` = "no-cache", 
                             `x-glb-token` = "mytoken"))

这非常直接(如果在发布问题之前已经完成 任何 研究)转换为:

httr::GET( 
  url = "https://api.cartolafc.globo.com/auth/liga/gurudocartola-com", 
  httr::add_headers(
    `Cache-Control` = "no-cache", 
    `x-glb-token` = "mytoken"
  ),
  query = list(
    `orderBy` = "campeonato",
    `page` = 1L
  )
)

反引号只是为了提醒我它们是参数(而且,它们有时包含破折号或其他强制反引号的字符)。