I'm new to this whole API thing and certainly don't understand the full logic, but hope you can help me out here.
Please note that I can only provide a generic example without a reproducible example since the site I want to access requires a paid login.
What I want to do is to delete certain values from a data column on the server.
The data on the server is a survey with each column representing a survey question. I now want to delete from the column "xyz" the case with a value of "abc".
The help page from said website provides the following help on how to delete data:
http://release.decipherinc.com/s/local/beacon.html#data-input-and-output-modify-data-delete
And this is how I tried to achieve deleting the data:
library(httr)
library(tidyverse)
library(jsonlite)
my_data <- data.frame(xyz = c("abc", "def", "ghi"))
todelete <- my_data %>%
filter(xyz == "abc")
my_platform_key <- "testkey"
# small function for the URL request
api_delete <- function(path)
{
url <- paste0("release.decipherinc.com/api/v1/", path)
add_headers(my_platform_key)
DELETE(url, add_headers('x-apikey' = my_platform_key))
}
delete_request <- api_delete("surveys/MYSURVEY/data/edit?mode=delete&key=xyz&data=todelete")
I guess the problem might be with how I specify the data
parameter. The help page says it needs to be an array, but not sure if simply passing a data frame is sufficient.
When I run this code followed up by: content(delete_request, encoding = "UTF-8")
the response code is 400, so indicating that it didn't work (obviously):
data: expected an array, not string
What do I need to change in my code so that it will work?
Update
Digging deeper into the topic I found some additional information, but it still doesn't work. According to some other SO post, I tried the following:
library(jsonlite)
library(httr)
args <- list(key = unbox("xyz"),
data = todelete,
mode = unbox("delete"))
body <- toJSON(args)
DELETE(url = "release.decipherinc.com/api/v1/surveys/MYSURVEY/data/edit",
add_headers('x-apikey' = my_platform_key),
body = body,
encode = "json")
I checked the content of body
and it looks exactly how it is described in the linked documentation, but it still doesn't work:
{"key":"xyz","data":[{"xyz":"abc"}],"mode":"delete"}
And the server response is:
Response [...URL...]
Date: 2020-04-04 17:50
Status: 400
Content-Type: application/json
Size: 72 B
{
"$error": "missing argument 'data'",
"$code": 400,
"extra": null
}
OK, I figured it out on my own with a lot of trial and error. I was missing one particular parameter in my function, i.e. content_type. The code below works:
library(httr)
library(tidyverse)
library(jsonlite)
my_data <- data.frame(xyz = c("abc", "def", "ghi"))
todelete <- my_data %>%
filter(xyz == "abc")
my_platform_key <- "testkey"
args <- list(key = unbox("xyz"),
data = todelete,
mode = unbox("delete"))
body <- toJSON(args)
DELETE(url = "release.decipherinc.com/api/v1/surveys/MYSURVEY/data/edit",
add_headers('x-apikey' = my_platform_key),
content_type("application/json"),
body = body,
encode = "json")