Hello RStudio Community , I have a question regarding JSON body responses on R using the httr package (specifically the PATCH()).
Objective
The idea is to update (or patch ) a JSON object in an API. To achieve this, I will use PATCH(). Using PATCH(), I have to send the necessary information to the API server. The API requires the body of my response to be of type JSON. The problem here is to replicate the JSON correctly.
Code Samples
The JSON I need to send has to look like this:
{
"data": {"someData": 012345}
}
The R code that I use to replicate this JSON is:
toJSON( list(`data`= list(`someData`=012345)) )
But, when I use the toJSON() I get the following:
{"data":{"someData":[12345]}}
The Problem
The problem here is in the [12345], I don't want that data to be inside an array. My response has to be like the first code sample.
Hey, the result of your reply lead me to the solution. While the use of paste() didn't worked (still got the 400 error) its return value (which is "{ \"app_metadata\": {\"tier\": \"basic\"} }" ) got me thinking: "Maybe the API server is not aware that my response is of type JSON".
httr has a content_type_json() function. This is used to specify that your body response is of type JSON.
Cool! The statement I suggested evaluates to exactly the response that you said you needed, but only if it is evaluated. Apparently being wrapped inside the PATCH() prevented that, so something extra would be needed to force evaluation. The solution you found looks like the right one.
I'm curious, does
body = cat("{",
'"data": {"someData": 012345}',
"}",
collapse = "\n")