As the title suggest, I am having a hard time querying an API with httr in R, which works well in Python using requests. I am not sure what I am doing wrong, and don't have much experience with these type of requests. I've been searching around, but can't seem to find the answer, any help would be appreciated. API endpoint documentation: https://docs.scifeon.com/developer/reference/http-api/endpoints/query
Works in python:
import requests
username = "myemail@web.com"
personal_access_token = "mytoken"
url = "https://provider/api/query/dataset"
header = {"Content-Type": "application/json"}
query = [
{
"eClass": "Fermentation",
"collection": "fermentations",
"filters": [{ "field": "attributes.experiment", "value": "expnumber"},],
}
]
response = requests.request("POST",
url,
headers=header,
json=query,
auth=(
username,
personal_access_token
)
)
response.json()
This python snippet gives the expected json output. However, this does not work in R:
library(httr)
library(jsonlite)
username = "myemail@web.com"
personal_access_token = "mytoken"
url = "https://provider/api/query/dataset"
query <- list(eClass = "Fermentation",
collection = "fermentations",
filters = list(field = "attributes.experiment", value = "expnumber"))
response <- POST(url,
add_headers("Content-Type" = "application/json"),
authenticate(username, personal_access_token),
body = query,
encode = 'json'
)
content(response)
The R response is:
[[1]]
[[1]]$code
[1] 1
[[1]]$message
[1] "Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray'."
[[1]]$hint
[1] ""
[[1]]$exception
[[1]]$exception$message
[1] "Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray'."
[[1]]$exception$stackTrace
[1] ""
[[1]]$exception$innerException
NULL
[[1]]$stacktrace
[1] ""