Skip to content

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

Open
Hashim1999164 wants to merge 2 commits into
expressjs:masterfrom
Hashim1999164:docs/note-undefined-body-v2
Open

docs: note that req.body may be undefined in v2#752
Hashim1999164 wants to merge 2 commits into
expressjs:masterfrom
Hashim1999164:docs/note-undefined-body-v2

Conversation

@Hashim1999164

@Hashim1999164Hashim1999164 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.

Fixes#632

Test plan

  • Confirm the new note reads clearly in the README API section
  • Confirm it matches the breaking change already listed in HISTORY.md for 2.0.0

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

@kilisamemarisaaakilisamemarisaaa 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?

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migration Guide V1 -> V2

2 participants

@Hashim1999164@kilisamemarisaaa