req.body undefined in Express— Fixed [Complete Guide 2026]
⚡ Quickest Fix — Two Lines
Add these two lines to your Express app BEFORE all routes:
const express = require('express')
const app = express()
// ← ADD THESE TWO LINES BEFORE ROUTES
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
// Routes come AFTER
app.post('/api/users', (req, res) => {
console.log(req.body) // now populated
res.json(req.body)
})Still undefined? Check that your frontend is sending Content-Type: application/json. See Fix 3.
Why Is req.body undefined in Express?
// console.log(req.body) inside your route:
undefined
// or an empty object even though you sent data:
{}Express does not parse request bodies by default. You must explicitly add middleware to tell Express how to read the request body. Without it, req.body is always undefined.
| Symptom | Cause | Fix |
|---|---|---|
| req.body is undefined | Body parser middleware missing | Jump → |
| req.body is undefined for some routes | Middleware added after routes | Jump → |
| req.body is {} (empty object) | Frontend not sending Content-Type | Jump → |
| req.body is undefined in old Express | Express below 4.16 | Jump → |
| req.body undefined for file uploads | multipart/form-data not handled | Jump → |
| 413 Request Entity Too Large | Body size limit exceeded | Jump → |
Add Body Parser Middleware
Express 4.16+ — no extra packages neededSince Express 4.16, body parsing is built in. You do not need to install any additional packages.
const express = require('express')
const app = express()
// Parse JSON bodies (application/json)
app.use(express.json())
// Parse URL-encoded bodies (application/x-www-form-urlencoded)
// e.g. HTML form submissions
app.use(express.urlencoded({ extended: true }))
// Routes AFTER middleware — always
app.post('/api/users', (req, res) => {
console.log(req.body) // { name: 'John', email: 'john@example.com' }
res.status(201).json({ created: req.body })
})
app.post('/contact', (req, res) => {
console.log(req.body) // form data from HTML form
res.json({ received: true })
})
app.listen(3000)What each middleware handles
| Middleware | Handles | Content-Type |
|---|---|---|
| express.json() | JSON data from fetch/axios | application/json |
| express.urlencoded() | HTML form submissions | application/x-www-form-urlencoded |
| multer (separate package) | File uploads | multipart/form-data |
Add both express.json() AND express.urlencoded() — they handle different request types and do not conflict with each other.
Middleware Must Go Before Routes
Works for some routes but not othersExpress applies middleware in the order it is defined. If body parser is added after a route, that route never has access to req.body.
// ❌ WRONG — body parser after routes
const app = express()
app.post('/users', (req, res) => {
console.log(req.body) // undefined!
res.json(req.body)
})
app.use(express.json()) // too late — route already defined above
// ✅ CORRECT — body parser first, routes second
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.post('/users', (req, res) => {
console.log(req.body) // populated!
res.json(req.body)
})The correct order: 1. Security middleware (helmet, cors), 2. Body parsers (express.json, urlencoded), 3. Logging middleware, 4. Routes, 5. Error handler.
Fix Frontend Content-Type Header
req.body is empty object {}Even with body parser middleware, req.body will be empty if your frontend does not send the correct Content-Type header. Express uses this header to decide which parser to apply.
fetch API — must set Content-Type manually
// ❌ WRONG — no Content-Type header
fetch('/api/users', {
method: 'POST',
body: JSON.stringify({ name: 'John' })
// missing Content-Type → req.body is {}
})
// ✅ CORRECT — Content-Type: application/json
fetch('/api/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ name: 'John' })
})Axios — sets Content-Type automatically
// ✅ Axios automatically sets Content-Type: application/json
// when you pass an object as the request body
axios.post('/api/users', { name: 'John' })
// ✅ Axios also handles arrays correctly
axios.post('/api/items', [1, 2, 3])HTML form — sets Content-Type automatically
<!-- HTML forms send application/x-www-form-urlencoded by default -->
<!-- express.urlencoded() handles this -->
<form method="POST" action="/contact">
<input name="name" value="John" />
<button type="submit">Submit</button>
</form>Debug: log the Content-Type header
app.post('/api/users', (req, res) => {
console.log('Content-Type:', req.headers['content-type'])
console.log('req.body:', req.body)
// Expected: Content-Type: application/json
// If undefined or different → fix the frontend
res.json({ received: req.body })
})Express Below 4.16 — Install body-parser
Older Express versionsIf you are using Express 4.15 or below (check: npm list express), the built-in body parser is not available. Install the separate body-parser package.
npm install body-parserconst express = require('express')
const bodyParser = require('body-parser')
const app = express()
// body-parser for older Express versions
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))Express 4.16+ is now almost universal. If you are on an older version, upgrading to the latest Express is a better long-term fix: npm install express@latest
File Uploads — multipart/form-data
req.body undefined for file upload routesexpress.json() and express.urlencoded() do NOT handle file uploads. For multipart/form-data (file uploads), you must use multer.
npm install multerconst multer = require('multer')
const upload = multer({ dest: 'uploads/' })
// Single file upload
app.post('/upload', upload.single('file'), (req, res) => {
console.log(req.file) // uploaded file info
console.log(req.body) // other form fields
res.json({ uploaded: req.file.filename })
})
// Multiple files
app.post('/photos', upload.array('photos', 5), (req, res) => {
console.log(req.files) // array of uploaded files
res.json({ count: req.files.length })
})
// Mixed: files + form fields
app.post('/profile', upload.fields([
{ name: 'avatar', maxCount: 1 },
{ name: 'gallery', maxCount: 10 }
]), (req, res) => {
console.log(req.files) // { avatar: [...], gallery: [...] }
console.log(req.body) // text fields from the form
res.json({ success: true })
})// FormData for file uploads
const formData = new FormData()
formData.append('file', fileInput.files[0])
formData.append('name', 'My Photo')
// Do NOT set Content-Type manually with FormData
// The browser sets it automatically with the correct boundary
fetch('/upload', {
method: 'POST',
body: formData
})Never set Content-Type manually when using FormData. The browser automatically adds the multipart/form-data boundary that multer needs to parse the file correctly.
Increase Body Size Limit (413 Error)
PayloadTooLargeError / 413 Request Entity Too LargePayloadTooLargeError: request entity too large
at readStream (/node_modules/raw-body/index.js:155:17)The default body size limit is 100kb. Large JSON payloads or base64-encoded files exceed this. Increase the limit:
// Increase limit for JSON bodies
app.use(express.json({ limit: '10mb' }))
// Increase limit for URL-encoded bodies
app.use(express.urlencoded({ extended: true, limit: '10mb' }))
// Or set a very high limit (use carefully — large payloads can be a DoS risk)
app.use(express.json({ limit: '50mb' }))Set the smallest limit that covers your use case. Very large body limits can be exploited for denial-of-service attacks. For file uploads over a few MB, use multer with disk storage instead of base64-encoded JSON.
req.body is Empty Object {}
Middleware working but body appears emptyIf req.body is {} (not undefined — actually an empty object), the middleware is working correctly but the frontend is either not sending data or sending with the wrong Content-Type.
Debug step by step
// Add this BEFORE your routes to debug all requests
app.use((req, res, next) => {
console.log('Method:', req.method)
console.log('Content-Type:', req.headers['content-type'])
console.log('Body:', req.body)
next()
})Common reasons for empty body
- Frontend sending GET request instead of POST — GET requests have no body
- Content-Type: text/plain instead of application/json — Express ignores it
- JSON.stringify() not called — sending [object Object] as text
- Empty object being sent: {} — this is valid, req.body will be {}
- Query parameters confused for body — use req.query not req.body for URL params
Debug Checklist
- 1
Is app.use(express.json()) present?
Add it before all routes
- 2
Is it placed BEFORE your routes?
Move it to the top of middleware chain
- 3
Is Content-Type: application/json set in the request?
Add to fetch headers or use axios
- 4
Is it a file upload? (multipart/form-data)
Use multer instead of express.json()
- 5
Is the body over 100kb?
Increase: app.use(express.json({ limit: '10mb' }))
- 6
Is it still {} after all fixes?
Log Content-Type header to confirm what is being sent
How to Prevent This Error
- Always add express.json() and express.urlencoded() at the top of your middleware chain
- Use a template or boilerplate that includes body parsers by default
- Always set Content-Type: application/json when sending JSON from fetch
- Use axios — it sets Content-Type automatically for object bodies
- Use multer for file uploads — express.json() cannot handle multipart/form-data
- Add a debug middleware in development to log Content-Type and body for every request
Frequently Asked Questions
Why is req.body undefined in Express?+−
Do I need to install body-parser in Express 4?+−
req.body is an empty object {} — why?+−
How do I receive file uploads in Express?+−
req.body works for some routes but not others?+−
Express body parser 413 error — how to fix?+−
Need Expert Help?
We Build Production-Grade Express & Node.js APIs
Softplix is a full-stack development agency specialising in Node.js backends. Dealing with Express configuration issues? We can help.
Talk to an Engineer