如何通过 Postman 发送 JSON 到 NodeJS Express Server?

How to send JSON to NodeJS Express Server through Postman?

我正在尝试从 Chrome 应用程序 'Postman' 通过 POST 发送 JSON object。

如果我将 header 设置为 'Content-Type: text/plain',则另一端的输出为空 object:

{}

如果我将 header 设置为 Content-Type: application/json,我会收到以下响应...

'Error: invalid json'.

键设置为ingredients,值设置为:

{ ["name" : "num1", "quantity" : "5"], ["name" : "num2", "quantity" : "2"], [ "name" : "num3", "quantity" : "8"]}

我在这里找到了它:

router.post('/add', jsonParser, function( req, res ) {

     if (!req.body) return res.sendStatus(400);    
     console.log( req.body );
});

您的 JSON 无效。重组为既能正确解析又能让你继续工作的东西。下面的示例应该可以正常工作,您的对象数组存储在 ingredients...

{
    "ingredients": [{
        "name": "num1",
        "quantity": "5"
    }, {
        "name": "num2",
        "quantity": "2"
    }, {
        "name": "num3",
        "quantity": "8"
    }]
}

您可以随意切换它,只要确保它能够解析即可。 JSONLint 将成为您的朋友。即使是没有命名标识符的普通数组也可以工作,而且我看得越多,它看起来就好像您只是将 {}[] 语法倒过来...

[{"name": "num1", "quantity": "5"}, {"name": "num2","quantity": "2"}, {"name": "num3","quantity": "8"}]

First of all You need to design data as per your Model . Use http://jsoneditoronline.org/ to format Json with out Syntax errors

Step 1:- Set Url parameters (if you have )

Step 2:- Set Standard Http headers

Example:- 1). Content-Type: application/json 2). Accept : application/json

Then Use http CRUD operations as per Your Requirement

You need to send Data to POST and PUT Actions Get - can get the json from api

Post Ex:- Select Post with API you see 3 options form-data x-www-form-urlencoded raw Select Option "raw" and Paste your Json content

祝你好运!!