close
Skip to content

feat(request): add req.hasBody getter#7206

Open
walle250ai wants to merge 1 commit intoexpressjs:masterfrom
walle250ai:feat/add-req-has-body
Open

feat(request): add req.hasBody getter#7206
walle250ai wants to merge 1 commit intoexpressjs:masterfrom
walle250ai:feat/add-req-has-body

Conversation

@walle250ai
Copy link
Copy Markdown

Summary

  • Add req.hasBody boolean getter to lib/request.js
  • Returns true if request carries a non-empty body: Content-Length > 0 OR Transfer-Encoding header present
  • Returns false for Content-Length: 0, missing content headers
  • Follows existing defineGetter pattern (same as req.fresh, req.xhr, etc.)
  • No new dependencies

Motivation

Express has no built-in way to check if a request has a body before parsing. Developers currently repeat req.headers["content-length"] checks across middleware. req.hasBody surfaces this as a first-class property.

Test plan

  • POST with Content-Length > 0 → true
  • GET with Content-Length: 0 → false
  • Request with Transfer-Encoding: chunked → true
  • Request with no content headers → false
  • All 4 tests pass: mocha --require test/support/env test/req.hasBody.js

🤖 Generated with Claude Code

Returns true if request has a non-empty body based on Content-Length
or Transfer-Encoding headers, without relying on external hasBody
utilities that treat Content-Length:0 as truthy.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@krzysdz
Copy link
Copy Markdown
Contributor

krzysdz commented Apr 27, 2026

I see two problems with this:

  1. given the name, one would assume that req.body is not empty when req.hasBody() is true, but req.body is created by middleware (e.g. body-parser), while this implements a much lower level primitive,
  2. type-is already implements this and it is an Express dependency through body-parser, which uses it to check if the request has a body.

By the way, I did not see anybody requesting adding this functionality directly to Express.

Sign up for free to 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.

2 participants