Reputation: 493
I am trying to send a JSON object through POST from the Chrome app 'Postman'.
If I set the header as 'Content-Type: text/plain'
, then the output at the other side is an empty object:
{}
If I set the header as Content-Type: application/json
, I receive the following response...
'Error: invalid json'.
The key is set as ingredients
and the value is set as:
{ ["name" : "num1", "quantity" : "5"], ["name" : "num2", "quantity" : "2"], [ "name" : "num3", "quantity" : "8"]}
and I catch it here:
router.post('/add', jsonParser, function( req, res ) {
if (!req.body) return res.sendStatus(400);
console.log( req.body );
});
Upvotes: 1
Views: 3972
Reputation: 1600
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
Good Luck !!
Upvotes: 0
Reputation: 16989
Your JSON is not valid. Restructure to something that will both parse correctly and work for you to continue. The below example should work fine, where your array of objects are stored in ingredients
...
{
"ingredients": [{
"name": "num1",
"quantity": "5"
}, {
"name": "num2",
"quantity": "2"
}, {
"name": "num3",
"quantity": "8"
}]
}
You can switch this up however you like, just ensure it parses. JSONLint will be your friend for that. Even a plain array without a named identifier will work as well, and the more I look at it, it appears as if you simply had your {}
and []
syntax backwards...
[{"name": "num1", "quantity": "5"}, {"name": "num2","quantity": "2"}, {"name": "num3","quantity": "8"}]
Upvotes: 3