Search code examples
rrcurl

How to use curl options in RCurl


How do you make the following request using the RCurl package?

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

Solution

  • Do you have to use the RCurl package? Here a solution using the curl package:

    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"
    #> }