Failed to decode JSON in HTTP request

Dmitri Zaitsev picture Dmitri Zaitsev · May 6, 2014 · Viewed 11.5k times · Source

I am using Parse Cloud Code to make a 'DELETE' HTTP Request to Delete Multiple Messages from Iron.io.

It is using exactly the same headers and url as 'GET' request to Get Message from the Queue:

    headers: {
        'Content-Type': 'application/json;charset=utf-8',
            'Authorization': 'OAuth ' + ironToken
      },

The 'GET' request does work, whether I put method: 'GET' or not inside Parse.Cloud.httpRequest(). It does work even if I send some data as body: (which are ignored).

However, for a 'DELETE' request, I need to send body:

 body: {
    'ids': ['someMessageId']
 }

And this requests fails with very unhelpful message:

{
"status":400,"headers":
 {"Access-Control-Allow-Origin":"*",
 "Connection":"keep-alive",
 "Content-Length":"32",
 "Content-Type":"application/json",
 "Date":"Tue, 06 May 2014 10:15:27 GMT"
},
"text":"{\"msg\":\"Failed to decode JSON.\"}",
"data":{"msg":"Failed to decode JSON."},
"buffer":[ ...],
"cookies":{}
}

Any idea why this happens and what else can I test?

Answer

thousandsofthem picture thousandsofthem · May 6, 2014
 body: {
    'ids': ['someMessageId']
 }

Is not valid json object. You need double quotes everywhere:

 "body": {
    "ids": ["someMessageId"]
 }