Skip to content

docs: note that req.body may be undefined in v2 - #752

Merged
UlisesGascon merged 2 commits into
expressjs:masterfrom
Hashim1999164:docs/note-undefined-body-v2
Oct 21, 2026
Merged

UlisesGascon merged 2 commits into
expressjs:masterfrom
Hashim1999164:docs/note-undefined-body-v2

Conversation

@Hashim1999164

@Hashim1999164 Hashim1999164 commented Jul 23, 2026 •

Copy link
Copy Markdown

Summary

  • Document the v1 to v2 behavior change where req.body stays undefined when no body is parsed, instead of always being initialized to {}.
  • Call out optional chaining / defaults for code that assumed an object.

Ref: #632

Document the v1 to v2 change where req.body stays undefined when no body
is parsed, instead of always being initialized to {}.

Fixes expressjs#632

@kilisamemarisaaa kilisamemarisaaa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "empty body" example is broader than v2's actual behavior. A request with a matching Content-Type: application/json and Content-Length: 0 is still considered to have a body, and the JSON parser returns {}; the existing test/json.js cases "should handle Content-Length: 0" and "should handle empty message-body" both assert that result.

The breaking change applies when no body is parsed, for example when the request has neither Content-Length nor Transfer-Encoding, or when Content-Type does not match. Could this example be narrowed so it does not imply that all zero-length JSON requests now leave req.body undefined?

@Hashim1999164

Copy link
Copy Markdown
Author

yeah my bad, empty json with content length 0 still parses to {}. i narrowed the note to the no body parsed cases, missing content length or transfer encoding, or a type that doesnt match.

@UlisesGascon UlisesGascon self-assigned this Oct 21, 2026
@UlisesGascon
UlisesGascon merged commit 402dbdc into expressjs:master Oct 21, 2026
2 checks passed
@UlisesGascon UlisesGascon mentioned this pull request Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3 participants