0

I'm posting a JSON message to an endpoint in my foxx microservice. It's unclear to me how to get the complete JSON in order to parse it:

router.post('/storeDataRecord', (req, res) => {

....

}).body('deviceData');

This is the json:

{
    “data”: [
        {
            “id”: “identifier”,
            “key1”: “value1”,
            “key2”: “value2”
        }
    ]
}

I've tried with

var request = req.body.deviceData;
var request = req.body.get('data');

can you help me to understand how to navigate the json?

alfven
  • 147
  • 8
  • Your JSON is invalid. You are using the wrong sort of quotes. – Quentin Jan 29 '17 at 17:29
  • Yes, the JSON is wrong. In any case, the right way is the following: var request = req.body. deviceData; and then to get the "id", var reqId = request.id; thanks – alfven Jan 29 '17 at 18:01
  • As they said, use `"` double quotes, not the `“`,`”` formatted ones you have there in your comment. Also try a `JSON.parse(req.body.deviceData)` if you think the input is in 'text' format and needs to be converted to JSON. – David Thomas Jan 30 '17 at 04:49

2 Answers2

0

Here is a minimal, complete, working example of a post-request route accepting a json body :

var joi = require('joi');
var processJson = function(jsonObject) {
    return JSON.stringify(jsonObject);
};
router.post('/start', function(req, res) {
    var result = processJson(req.body);
    res.json({'result': result});
}).summary('Json example').body(joi.object().unknown(true), ['json']);

Notice that the accepted content-type is declared (['json'], shorthand for ['application/json'])

Of course this assumes the data you post is actually valid JSON, but if it is not, you should see errors on the sending side (debug into the sending code and inspect the data you pass), or at least an error from arangodb indicating invalid data in the request.

Tom Regner
  • 6,856
  • 4
  • 32
  • 47
0

First validate your JSON data with https://jsonformatter.curiousconcept.com/

Haseb Ansari
  • 587
  • 1
  • 7
  • 23