balderdashy/sails · error

Unable to parse HTTP body- error occurred ::

Error message

Unable to parse HTTP body- error occurred :: 

What it means

This log/error fires when Sails' HTTP body-parser middleware throws while parsing the request body. Sails builds the message 'Unable to parse HTTP body- error occurred :: <stack>' and, in production, responds with an empty 400; in development it echoes the error stack back.

Source

Thrown at lib/hooks/http/get-configured-http-middleware-fns.js:164

    compress: IS_NODE_ENV_PRODUCTION && require('compression')(),


    // Configures the middleware function used for parsing the HTTP request body, if enabled.
    bodyParser: (function() {

      var opts = {};
      var fn;

      opts.onBodyParserError = function (err, req, res, next) {// eslint-disable-line no-unused-vars
        // Note that we _need_ all four arguments in order for this function
        // to have special meaning as an error handler (i.e. to Express)

        var bodyParserFailureErrorMsg = 'Unable to parse HTTP body- error occurred :: ' + util.inspect((err&&err.stack)?err.stack:err, false, null);
        sails.log.error(bodyParserFailureErrorMsg);
        if (IS_NODE_ENV_PRODUCTION) {
          return res.status(400).send();
        }
        return res.status(400).send(bodyParserFailureErrorMsg);
      };

      // Handle original bodyParser config:
      ////////////////////////////////////////////////////////
      // If a body parser was configured, use it
      if (sails.config.http.bodyParser) {
        fn = sails.config.http.bodyParser;
        return fn(opts);
      } else if (sails.config.http.bodyParser === false) {
        // Allow for explicit disabling of bodyParser using traditional
        // `express.bodyParser` conf
        return undefined;
      }

      // Default to built-in bodyParser:
      fn = require('skipper');
      return fn(opts);

View on GitHub (pinned to 7b76422cc2)

Solutions

  1. Inspect the logged stack to identify the parse failure and fix the client payload to be valid for its Content-Type.
  2. Increase or configure bodyParser limits in config/http.js (e.g. sails.config.http.bodyParser options like limit).
  3. Ensure the client sends the correct Content-Type header matching the actual body format.
  4. Remove/disable body parsing for routes that expect raw or non-body requests.

Example fix

// before (config/http.js)
bodyParser: true
// after
bodyParser: require('skipper')({ limit: '10mb' })
Defensive patterns

Strategy: try-catch

Validate before calling

function isValidJson(str) {
  if (typeof str !== 'string') return false;
  try { JSON.parse(str); return true; } catch { return false; }
}
// call before sending: if (!isValidJson(payload)) fix payload before POST

Type guard

function hasValidContentType(req) {
  const ct = (req.headers['content-type'] || '').toLowerCase();
  return ct.includes('application/json') || ct.includes('application/x-www-form-urlencoded') || ct.includes('multipart');
}

Try / catch

try {
  const res = await fetch(url, { method: 'POST', body: JSON.stringify(data), headers: { 'Content-Type': 'application/json' } });
  if (res.status === 400) {
    const body = await res.text();
    if (body.startsWith('Unable to parse HTTP body')) console.error('Server could not parse request body:', body);
  }
} catch (err) { console.error(err); }

Prevention

When it happens

Trigger: A request with Content-Type application/json (or urlencoded/multipart) whose body is malformed or truncated; oversized bodies exceeding bodyParser limits; wrong charset; or a client claiming JSON but sending invalid syntax.

Common situations: Clients posting manually-crafted JSON with trailing commas or single quotes; upload size limit exceeded (payload too large); proxy truncating chunked bodies; tests sending no body while setting JSON content type.

Understand the failure class

Related errors


AI-assisted analysis of balderdashy/sails@7b76422cc2 (2026-09-01). Data as JSON: /api/errors/6bbb37a255402b39. Report an issue: GitHub.