如何在RCurl中使用curl选项

时间:2018-06-22 03:09:38

标签: r rcurl

如何使用RCurl包发出以下请求?

curl -X GET "https://api.youneedabudget.com/v1/budgets/XXXXX/accounts" \
     -H "accept: application/json" -H "Authorization: Bearer XXXXX"

1 个答案:

答案 0 :(得分:0)

您是否必须使用RCurl软件包?这是使用curl软件包的解决方案:

library(curl)
h <- new_handle()
handle_setheaders(h,
                  "accept" = "application/json",
                  "Authorization" = "Bearer XXXXX"
)

req <- curl_fetch_memory("http://httpbin.org/get", handle = h)
jsonlite::prettify(rawToChar(req$content))
#> {
#>     "args": {
#> 
#>     },
#>     "headers": {
#>         "Accept": "application/json",
#>         "Accept-Encoding": "gzip, deflate",
#>         "Authorization": "Bearer XXXXX",
#>         "Connection": "close",
#>         "Host": "httpbin.org",
#>         "User-Agent": "R (3.5.0 x86_64-pc-linux-gnu x86_64 linux-gnu)"
#>     },
#>     "origin": "77.180.186.114",
#>     "url": "http://httpbin.org/get"
#> }