I'm trying to create a JSON object from the payload in a POST request but can't seem to get it working. I'm using BaasBox and I've created a JavaScript plugin that looks like this:
http().post(function(req){
Box.log(req.method + " received: " + req);
var jsonObject = JSON(req.body);
Box.log("JSON parsed successfully");
var message = new Object();
message.message = jsonObject["message"];
message.firstname = jsonObject["firstname"];
message.lastname = jsonObject["lastname"];
var doc = Box.Documents.save("Messages",message);
Box.log("Messages created: " + doc.id);
return {status: 200, content: message};
});
It's a simple script that attempts to create a JSON object from the request body. The JSON from the iOS client application looks like this:
let json = "{ \"firstname\" : \"John\" , \"lastname\" : \"Jones\" , \"message\" : \"Hello there\" }"
Now here's the interesting part: If I just store the request body like this instead, then the JSON is successfully stored as a Document.
http().post(function(req){
Box.log(req.method + " received: " + req);
var doc = Box.Documents.save("Messages",req.body);
Box.log("Messages created: " + doc.id);
return {status: 200, content: req.body};
});
How can I create a JSON object from the request body?
Turns out that the req.body wasn't an actual string. Here's the updated code that works: