{"record":{"id":"6bbb37a255402b39","repo":"balderdashy/sails","slug":"unable-to-parse-http-body-error-occurred","errorCode":null,"errorMessage":"Unable to parse HTTP body- error occurred :: ","messagePattern":"Unable to parse HTTP body- error occurred :: ","errorType":"http","errorClass":null,"httpStatus":400,"severity":"error","filePath":"lib/hooks/http/get-configured-http-middleware-fns.js","lineNumber":164,"sourceCode":"    compress: IS_NODE_ENV_PRODUCTION && require('compression')(),\n\n\n    // Configures the middleware function used for parsing the HTTP request body, if enabled.\n    bodyParser: (function() {\n\n      var opts = {};\n      var fn;\n\n      opts.onBodyParserError = function (err, req, res, next) {// eslint-disable-line no-unused-vars\n        // Note that we _need_ all four arguments in order for this function\n        // to have special meaning as an error handler (i.e. to Express)\n\n        var bodyParserFailureErrorMsg = 'Unable to parse HTTP body- error occurred :: ' + util.inspect((err&&err.stack)?err.stack:err, false, null);\n        sails.log.error(bodyParserFailureErrorMsg);\n        if (IS_NODE_ENV_PRODUCTION) {\n          return res.status(400).send();\n        }\n        return res.status(400).send(bodyParserFailureErrorMsg);\n      };\n\n      // Handle original bodyParser config:\n      ////////////////////////////////////////////////////////\n      // If a body parser was configured, use it\n      if (sails.config.http.bodyParser) {\n        fn = sails.config.http.bodyParser;\n        return fn(opts);\n      } else if (sails.config.http.bodyParser === false) {\n        // Allow for explicit disabling of bodyParser using traditional\n        // `express.bodyParser` conf\n        return undefined;\n      }\n\n      // Default to built-in bodyParser:\n      fn = require('skipper');\n      return fn(opts);\n","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/balderdashy/sails/blob/7b76422cc27823df033572bdda5c4910a68b697f/lib/hooks/http/get-configured-http-middleware-fns.js#L146-L182","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Inspect the logged stack to identify the parse failure and fix the client payload to be valid for its Content-Type.","Increase or configure bodyParser limits in config/http.js (e.g. sails.config.http.bodyParser options like limit).","Ensure the client sends the correct Content-Type header matching the actual body format.","Remove/disable body parsing for routes that expect raw or non-body requests."],"exampleFix":"// before (config/http.js)\nbodyParser: true\n// after\nbodyParser: require('skipper')({ limit: '10mb' })","handlingStrategy":"try-catch","validationCode":"function isValidJson(str) {\n  if (typeof str !== 'string') return false;\n  try { JSON.parse(str); return true; } catch { return false; }\n}\n// call before sending: if (!isValidJson(payload)) fix payload before POST","typeGuard":"function hasValidContentType(req) {\n  const ct = (req.headers['content-type'] || '').toLowerCase();\n  return ct.includes('application/json') || ct.includes('application/x-www-form-urlencoded') || ct.includes('multipart');\n}","tryCatchPattern":"try {\n  const res = await fetch(url, { method: 'POST', body: JSON.stringify(data), headers: { 'Content-Type': 'application/json' } });\n  if (res.status === 400) {\n    const body = await res.text();\n    if (body.startsWith('Unable to parse HTTP body')) console.error('Server could not parse request body:', body);\n  }\n} catch (err) { console.error(err); }","preventionTips":["Always send bodies that match the declared Content-Type header.","Respect server body-size limits; set an explicit limit in config/http.js and enforce it client-side.","Validate JSON payloads with a parser before sending in tests and scripts.","Beware proxies/load balancers that truncate large chunked uploads."],"tags":["http","body-parser","bad-request","middleware"],"backgroundTag":"malformed-request-body","analyzedSha":"7b76422cc27823df033572bdda5c4910a68b697f","analyzedAt":"2026-09-01T04:01:57.104Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}