Hello, I got the following error when try to POST content via postman using Sails Blueprints. GET and DELETE methods works fine.
So when I try to update or create a model, I got the following error:
error: Unable to parse HTTP body- error occurred :: 'Error: invalid json\n at parse (/Users/Ytzvan/myapp/node_modules/sails/node_modules/skipper/node_modules/connect/node_modules/body-parser/lib/types/json.js:62:15)\n at /Users/Ytzvan/myapp/node_modules/sails/node_modules/skipper/node_modules/connect/node_modules/body-parser/lib/read.js:91:18\n at IncomingMessage.onEnd (/Users/Ytzvan/myapp/node_modules/sails/node_modules/skipper/node_modules/connect/node_modules/body-parser/node_modules/raw-body/index.js:136:7)\n at IncomingMessage.g (events.js:180:16)\n at IncomingMessage.emit (events.js:92:17)\n at _stream_readable.js:943:16\n at process._tickDomainCallback (node.js:463:13)'
Thanks.
Sounds like you are sending invalid JSON data in the body. Paste an example of what data you are sending.
Using postman form-data the result is the previous error, but if I send a raw json like this:
{
"name": "Beach Trip",
"description": "Beach Trip",
"city": "Beach Trip",
"country": "Panama",
"min": null,
"max": null,
"departure": null,
"arrival": null,
"price": null,
"duration": null,
"transportation": null,
"food": null,
"pictures": null,
"insurance": null,
"category": null,
"location": "undefined, undefined",
"latitude": null,
"longitude": null
}
Works fine.
Sounds like you are sending Content-Type headers with application/json for your form encoded data. That would explain why it works with json data.
For using form data try setting your Content-Type headers to application/x-www-form-urlencoded.
Thanks for posting, @ytzvan. I'm a repo bot-- nice to meet you!
It has been 60 days since there have been any updates or new comments on this page. If this issue has been resolved, feel free to disregard the rest of this message. On the other hand, if you are still waiting on a patch, please:
Thanks so much for your help!
Hi All,
My request looks like:
{"identifier":\,"password":}
I need to write validation for this, for this i wrote "reqbodyvalidation" policy something like this and applied to the method.
function isValidJson(json) {
try {
JSON.parse(json);
return true;
} catch (e) {
return false;
}
}
if (!req.body && _.isEmpty(req.body)) {
return res.status(400).send({error:'Req Body should not be empty'});
}else{
if(isValidJson(req.body)){
next();
}else{
return res.status(400).send({error:'Not a valid JSON'});
}
}
and Policies:
userLogin: ['reqBodyValidation','passport','ModelPolicy', 'AuditPolicy']
Can anyone guide me to solve this as i feel it is not getting upto this place prior to this event. its throwing error:
error: Unable to parse HTTP body- error occurred :: 'SyntaxError: Unexpected token \n at Object.parse (native)\n at parse ............................
Thanks so much for your help!
@ayyappaappana you will need to handle this error within the http.js file. See https://github.com/balderdashy/sails-docs/blob/1.0/reference/sails.config/sails.config.http.md#customizing-the-body-parser for more information on configuring the default body parser, Skipper.
Most helpful comment
Sounds like you are sending
Content-Typeheaders withapplication/jsonfor your form encoded data. That would explain why it works with json data.For using form data try setting your
Content-Typeheaders toapplication/x-www-form-urlencoded.