Making a GET request in R
Asked Answered
N

4

8

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?

Narcissus answered 22/2, 2016 at 17:22 Comment(0)
A
3

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
Ammonify answered 23/2, 2016 at 15:38 Comment(2)
this library is so cool! What has been your experience with it? do you always get good working curl conversions?Narcissus
It 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.Ammonify
Q
9
httr::GET('https://this.url.api.com:334/api/endpoint', 
      accept_json(), 
      add_headers('Authorization' = 'Bearer 31232187asdsadh23187'))

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

Quaky answered 22/2, 2016 at 20:28 Comment(2)
The curl converter is really cool! I will be trying this tomorrow and will get back to you to let you know whether it worked!Narcissus
sorry 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!Narcissus
A
3

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
Ammonify answered 23/2, 2016 at 15:38 Comment(2)
this library is so cool! What has been your experience with it? do you always get good working curl conversions?Narcissus
It 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.Ammonify
H
1

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.

Higbee answered 16/12, 2019 at 14:19 Comment(0)
P
0

The httr2 package (a rewrite of httr) has the curl_translate function which takes a curl command generated from copy as curl in a browser's developer tools and translates it into a httr2 request. It is influenced by the curlconverter function cited in other answers.

resp <- 
  request("https://mcmap.net/q/1287076/-making-a-get-request-in-r") |> 
  req_headers(
    `User-Agent` = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0",
    Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
    `Accept-Language` = "en-US,en;q=0.5",
    `Accept-Encoding` = "gzip, deflate, br",
    `Upgrade-Insecure-Requests` = "1",
    DNT = "1",
    `Sec-GPC` = "1",
  ) |> 
  req_perform()
Pretentious answered 26/1 at 15:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.