Search code examples
rrcurlhttr

Reading API - code from Curl to R


I am trying to read a json from an authenticated API using R, but not sucessfully.

I have the Curl code and tried to convert it to R using "curlconverter" library and also tried to get it using "httr" library.

curl -X GET \
  'https://api.cartolafc.globo.com/auth/liga/gurudocartola-com?orderBy=campeonato&page=1' \
  -H 'Cache-Control: no-cache' \
  -H 'x-glb-token: mytoken'

I would appreciate a solution to write this code in R.


Solution

  • library(curlconverter) # devtools::install_github("hrbrmstr/curlconverter")
    
    u <- "curl -X GET 'https://api.cartolafc.globo.com/auth/liga/gurudocartola-com?orderBy=campeonato&page=1' -H 'Cache-Control: no-cache' -H 'x-glb-token: mytoken'"
    
    straighten(u) %>% 
      make_req()
    

    That makes:

    httr::VERB(verb = "GET", url = "https://api.cartolafc.globo.com/auth/liga/gurudocartola-com?orderBy=campeonato&page=1", 
               httr::add_headers(`Cache-Control` = "no-cache", 
                                 `x-glb-token` = "mytoken"))
    

    which very straightforwardly (if one has done any research before posting a question) translates to:

    httr::GET( 
      url = "https://api.cartolafc.globo.com/auth/liga/gurudocartola-com", 
      httr::add_headers(
        `Cache-Control` = "no-cache", 
        `x-glb-token` = "mytoken"
      ),
      query = list(
        `orderBy` = "campeonato",
        `page` = 1L
      )
    )
    

    The back-ticks are there solely to remind me they are parameters (and, they sometimes contain dashes or other chars which force a back-tick quote).