简体   繁体   English

arangodb,如何从发布请求中检索JSON

[英]arangodb, How to retrieve JSON from Post request

I'm posting a JSON message to an endpoint in my foxx microservice. 我将JSON消息发布到foxx微服务中的端点。 It's unclear to me how to get the complete JSON in order to parse it: 我还不清楚如何获取完整的JSON以便对其进行解析:

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

....

}).body('deviceData');

This is the json: 这是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? 您能帮我了解如何浏览json吗?

Here is a minimal, complete, working example of a post-request route accepting a json body : 这是接受json主体的请求后路由的最小,完整,有效的示例:

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'] ) 请注意,已声明了可接受的内容类型( ['json'] ,是['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. 当然,这假设您发布的数据实际上是有效的JSON,但如果不是,则应该在发送方看到错误(调试发送代码并检查您传递的数据),或者至少是arangodb的错误指示无效请求中的数据。

首先使用https://jsonformatter.curiousconcept.com/验证您的JSON数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM