Skip to content

Express 5.1: req.body is undefined when using express.json() for requests that do not have a request body #6432

Closed
@booherbg

Description

@booherbg

Simply put, with express 5.1 req.body is undefined when not explicitly including a request body. This is unexpected! In express 4.x, req.body is guaranteed to be at least {} even when the request body is missing.

Express 5.1 Bug Report: express.json() Middleware Body Initialization

Environment information

Version:

"dependencies": {
  "express": "5.1.0"  // Broken behavior
  "express": "4.21.2" // Working behavior
}

Platform:

$ uname -a
 24.2.0 Darwin Kernel Version 24.2.0: Fri Dec  6 18:40:14 PST 2024; root:xnu11215.61.5~2/RELEASE_ARM64_T8103 arm64

Node.js version:

$ node -v
v22.9.0

What steps will reproduce the bug?

  1. Create minimal Express server:
const express = require('express');
const app = express();

app.use(express.json());

app.post('/test', (req, res) => {
  console.log('Body:', req.body);
  res.json({ receivedBody: req.body });
});

app.listen(3000, () => console.log('Server running on port 3000'));
  1. Send POST request with no body:
$ npm install express@5
$ curl -X POST http://localhost:3000/test
Server Output: `Body: undefined`
$ curl -X POST http://localhost:3000/test -H "Content-Type: application/json"
Server Output: `Body: undefined`

$ npm install express@4
$ curl -X POST http://localhost:3000/test
Server Output: `Body: {}`
$ curl -X POST http://localhost:3000/test -H "Content-Type: application/json"
Server Output: `Body: {}`
  1. Observe behavior:
  • Express 4.x: req.body is {}
  • Express 5.x: req.body is undefined

Expected Behavior

When a POST request is made with no body (with or without Content-Type: application/json), the express.json() middleware should initialize req.body to an empty object {}, as it did in Express 4.x.

Actual Behavior

In Express 5.x, req.body is undefined when no body is sent, even when the request includes Content-Type: application/json.

This change breaks backward compatibility and affects common REST API patterns where routes expect req.body to be an object, even if empty.

Example output:

$ npm install express@5
$ curl -X POST http://localhost:3000/test
# Server Output: `Body: undefined`
$ npm install express@4
# Server Output: `Body: {}`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions