Home > Software design >  R httr GET doesn't like time format
R httr GET doesn't like time format

Time:10-13

I want to download data from a website using an api. It looks like the following but with the XXXs, YYYs and ZZZs replaced with api codes.

https://api.ecowitt.net/api/v3/device/history?application_key=XXXX&api_key=-YYY&mac=ZZZ&start_date=2022-06-01 00:00:00&end_date=2022-09-27 00:00:00&cycle_type=30min&call_back=outdoor,indoor.humidity

When I put this url into a web browser I get a page full of data so the server seems to be happy with it. When I name it "url_complete" and run

response <- httr::GET(url_complete) I get this error message

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

With the start and end dates and times removed from the above url, The response Status is 200.

response <- httr::GET(test_url)

content(response, "text") [1] "{"code":40000,"msg":"start_date require","time":"1664309668","data":[]}"

CodePudding user response:

The solution was to pull the date strings out of the url and add them with a query as follows

response <- httr::GET(url_complete_noDates, add_headers(accept = 'application/json'), query = list(start_date = "2022-06-01 00:00:00", end_date = "2022-09-27 00:00:00"))

What this does is add some code to replace the special characters in the date/time string - space and colon.

start_date=2022-06-01 00:00:00&end_date=2022-09-27 00:00:00
  • Related