0

I'm trying to do a GET request by connecting to a REST API. I'm pretty new on how to handle API's. I was given an API token and a password for authentication purposes. I'm using R and Python both to connect but failing in both. I've used the basic code below in R :

 library(httr)
library(jsonlite)
token<- 'XXX'
password<- 'XXX'
r1 <-GET("https://api.****.com/v1/Reporting/BalanceSheet/? query parameters"
         , add_headers(token,password))

r1

In the above code in place '****' I have the website name and all parameters mentioned instead of 'quers parameters'. When I run the 5th line of code it throws me the below error:

Error in curl::curl_fetch_memory(url, handle = handle) : 
  URL using bad/illegal format or missing URL

I'm not sure how to use the token and it's purpose...I'm more like assuming it to be the username.

Any lead would be appreciated. Thanks.

  • 1
    Possible duplicate of [Error in curl::curl\_fetch\_memory(url, handle = handle) : Couldn't connect to server in R: oauth2.0\_token())](https://stackoverflow.com/questions/39285570/error-in-curlcurl-fetch-memoryurl-handle-handle-couldnt-connect-to-ser). As suggested at this link, if you're at work, you may be behind a proxy. – Trenton McKinney Sep 27 '19 at 23:44

1 Answers1

1

First, spaces aren't allowed in URLs. Does it work without them?

Second, I think that the query, starting with ?, can't ever come after a slash. So it should be BalanceSheet?query_parameters

Also, it would be cleaner to use the path and query arguments to GET(), rather than one big URL:

GET("https://api.****.com", 
    path  = "/v1/Reporting/BalanceSheet",
    query = "query_parameters", 
    add_headers(token, password))

See the help for the ... in GET(), which leads you to ?modify_url

DHW
  • 1,157
  • 1
  • 9
  • 24