在 R 中发起 GET 请求

Making a GET request in R

我一直在玩 httr 和 rcurl,但无法将以下 curl GET 请求转换为 R:

curl -X GET --header 'Accept: application/json' --header 'Authorization: Bearer 31232187asdsadh23187' 'https://this.url.api.com:334/api/endpoint'

特别是,我在传递授权选项时遇到了一些麻烦,因为我无法在两个库中找到等效参数。这可能是一个习惯 header 也许吧?

httr::GET('https://this.url.api.com:334/api/endpoint', 
      accept_json(), 
      add_headers('Authorization' = 'Bearer 31232187asdsadh23187'))

另见 https://github.com/hrbrmstr/curlconverter

试用新的和进一步改进的 curlconverter 包。它将接受一个 curl 请求并输出一个 httr 命令。

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

library(curlconverter)

curlExample <- "curl -X GET --header 'Accept: application/json' --header 'Authorization: Bearer 31232187asdsadh23187' 'https://this.url.api.com:334/api/endpoint'"

resp <- make_req(straighten(curlExample))
resp

我同意斯科特的回答。我不太了解 curlconverter 的维护和正式性,但为了完成 httr 功能,我将再添加几行。

getInfoInJson <- httr::GET('https://this.url.api.com:334/api/endpoint', 
      accept_json(), 
      add_headers('Authorization' = 'Bearer 31232187asdsadh23187'))

 #safe the info in a json object 
 jsonInfoImg <- content(getInfoInJson, type="application/json")

希望对您有所帮助。