Making a GET request in R
14,357
Solution 1
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
Solution 2
httr::GET('https://this.url.api.com:334/api/endpoint',
accept_json(),
add_headers('Authorization' = 'Bearer 31232187asdsadh23187'))
See also https://github.com/hrbrmstr/curlconverter
Solution 3
I agree with sckott answer. I don't know too much about the maintenance and officiallity of curlconverter, but to complete httr function i will add few lines more.
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")
Hope it helps.
Author by
jgozal
Updated on June 27, 2022Comments
-
jgozal almost 2 years
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?
-
jgozal about 8 yearsThe curl converter is really cool! I will be trying this tomorrow and will get back to you to let you know whether it worked!
-
jgozal about 8 yearsthis library is so cool! What has been your experience with it? do you always get good working curl conversions?
-
jgozal about 8 yearssorry it took some time to get back to you. This request worked! although the curl converter that JackStat suggested makes making requests in R a piece of cake!
-
JackStat about 8 yearsIt is definitely a package that I foresee getting a lot of use. It is fairly new but I find it quite easy to pop in my curl requests and get a good chunk of httr code that I can use.