Sails: Error at POST, PUT

Created on 17 Jul 2015  路  6Comments  路  Source: balderdashy/sails

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.

more info please

Most helpful comment

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.

All 6 comments

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:

  • review our contribution guide to make sure this submission meets our criteria (only _verified bugs_ with documented features, please; no questions, commentary, or bug reports about undocumented features or unofficial plugins)
  • create a new issue with the latest information, including updated version details with error messages, failing tests, and a link back to the original issue. This allows GitHub to automatically create a back-reference for future visitors arriving from search engines.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

randallmeeker picture randallmeeker  路  4Comments

Sytten picture Sytten  路  4Comments

pawankorotane picture pawankorotane  路  3Comments

3imed-jaberi picture 3imed-jaberi  路  3Comments

edy picture edy  路  4Comments