Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a GET request in R

Tags:

http

r

rcurl

httr

I've been playing a little with httr and rcurl and cannot manage to translate the following curl GET request into R:

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

Particularly, I've been having some trouble to pass the Authorization option as I was not able to find the equivalent parameter in neither of the libraries. It might be a custom header maybe?

like image 930
jgozal Avatar asked Apr 25 '26 12:04

jgozal


2 Answers

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

See also https://github.com/hrbrmstr/curlconverter

like image 160
sckott Avatar answered Apr 27 '26 02:04

sckott


Try out the new and further improving curlconverter package. It will take a curl request and output an httr command.

#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
like image 39
JackStat Avatar answered Apr 27 '26 01:04

JackStat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!