Edit: Margusl's answer worked perfectly! Thanks very much Margus, I was not aware that httr had been replaced by httr2 as I seem to have been following an outdated tutorial. Thanks for taking the time to explain!!
I am trying to call a REST API in R and having difficulty with a parameter.
The API is from Todoist, a todo list/task manager (documentation here). My goal is to collect a set number of completed tasks for documentation.
I am able to connect to the API and retrieve data.
The issue is that, by default, the API returns 30 completed tasks. Using curl in cmd prompt, \ -d limit=200 can be added to the end to change the limit to 200 tasks, and I am indeed able to do so. For example, the following code in cmd produces the desired result of 200 tasks: curl https://api.todoist.com/sync/v9/completed/get_all \ -H "Authorization: Bearer 000api_key000" \ -d limit=200
In R, however, I cannot figure out how to do it. I have tried multiple combinations of adding limit = 200 with add_header() or paste() with no success. Fundamentally, I am missing something about how to transfer the curl -d argument into an acceptable argument in R.
Below is my code that successfully gets 30 completed tasks (with my API key removed). Any advice on how to pass on the API's limit parameter as described above would be very much appreciated.
library(httr)
library(jsonlite)
raw.result <- GET(url = "https://api.todoist.com",
path = "/sync/v9/completed/get_all",
add_headers(Authorization=paste("Bearer 000api_key000")))
When there's
-d/--datain acurlcommand, it's actually aPOSTcall with form data (i.e. it will not work withhttr::GET())httr2, the successor tohttr, comes withcurl_translate()tool that tries to translatecurlcommands for you, the result is often bit raw, but should give you the right direction:To check what would be sent to the server, we can temporarily replace
req_perform()withreq_dry_run():And as there are proper methods for bearer token and form body, one could write that same request as:
If you need some help translating to
httr, there's also https://curlconverter.com/r/ , which would return: