在R中发出GET请求

问题描述 投票:2回答:2

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

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

特别是,我在传递Authorization选项方面遇到了一些麻烦,因为我无法在两个库中找到等效参数。可能是自定义标题?

r http rcurl httr
2个回答
1
投票

尝试新的和进一步改进的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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.