API Reference

body-parser

Express and Connect middleware that parses JSON, urlencoded, raw, and text request bodies onto req.body.

Official Documentation ↗
CompatibilityThis documentation covers version v2.4.0. Check the official docs for the latest API surface.

Installation

npmnpm install body-parser

Methods

body-parser reads the request stream, decodes it according to Content-Type, and assigns the result to req.body.

Key Features

  • Independent parsers for JSON, urlencoded, raw, and text
  • Configurable size limits, strictness, and inflate behavior
  • Raw parser preserves bytes for signature verification

Usage Notes

Pin a version and check the official docs for the API surface you depend on before upgrading.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://expressjs.com/en/resources/middleware/body-parser.html · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:48:28.676Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

Security notes

Quoted verbatim from the official documentation; this is not a complete threat model. 来源:https://expressjs.com/en/resources/middleware/body-parser.html · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:48:28.676Z.

FAQ

Is body-parser still needed with Express 4.16+?
No for JSON and urlencoded. Express bundles express.json() and express.urlencoded(), which behave almost identically. Use body-parser for raw or text bodies.
How do I verify webhook signatures?
Use bodyParser.raw() so the exact bytes are preserved, compute the HMAC over req.body, and only then parse the payload as JSON.
Why does req.body come back empty?
The Content-Type header must match the parser. A mismatched type leaves req.body undefined, so check it before reading fields.