From 79d65ba54728491bcd0d46b06a8f2af7d67aa119 Mon Sep 17 00:00:00 2001 From: Hashim Khan Date: Thu, 23 Jul 2026 05:46:35 +0500 Subject: [PATCH 1/2] docs: note that req.body may be undefined in v2 Document the v1 to v2 change where req.body stays undefined when no body is parsed, instead of always being initialized to {}. Fixes #632 --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 335743d2..0ea58cba 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,12 @@ The `bodyParser` object exposes various factories to create middlewares. All middlewares will populate the `req.body` property with the parsed body when the `Content-Type` request header matches the `type` option. +**Note** As of v2, `req.body` is left `undefined` when no body is parsed (for +example an empty body or a non-matching `Content-Type`). In v1, `req.body` was +always initialized to `{}`. Code that assumes `req.body` is always an object +should use optional chaining (for example `req.body?.username`) or set a +default. + The various errors returned by this module are described in the [errors section](#errors). From cd776eae0d3b39368e4bdb51045e78b8c10dfe29 Mon Sep 17 00:00:00 2001 From: Hashim1999164 <64767361+Hashim1999164@users.noreply.github.com> Date: Tue, 1 Sep 2026 20:51:37 +0500 Subject: [PATCH 2/2] Narrow the v2 req.body undefined docs note --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0ea58cba..5507cc6e 100644 --- a/README.md +++ b/README.md @@ -66,10 +66,10 @@ middlewares will populate the `req.body` property with the parsed body when the `Content-Type` request header matches the `type` option. **Note** As of v2, `req.body` is left `undefined` when no body is parsed (for -example an empty body or a non-matching `Content-Type`). In v1, `req.body` was -always initialized to `{}`. Code that assumes `req.body` is always an object -should use optional chaining (for example `req.body?.username`) or set a -default. +example when the request has neither `Content-Length` nor `Transfer-Encoding`, +or when `Content-Type` does not match). In v1, `req.body` was always initialized +to `{}`. Code that assumes `req.body` is always an object should use optional +chaining (for example `req.body?.username`) or set a default. The various errors returned by this module are described in the [errors section](#errors).