HTTP 413 Content Too Large — Request Entity Too Large Fix
The request body is larger than the server is willing or able to process. The server may close the connection or return a Retry-After header. Previously called 'Request Entity Too Large' in older HTTP specs.
When to Return 413
Typically configured at the server/proxy level (nginx client_max_body_size, Express bodyParser limit). Return 413 from application code when you enforce file size limits for uploads.
Common Causes
- File upload exceeding server limit
- JSON body larger than parser limit
- nginx client_max_body_size reached
- Attempting to upload video to an image-only endpoint
HTTP Response Example
HTTP/1.1 413 Content Too Large
Content-Type: application/json
{"error": "File too large. Maximum size is 5 MB."}Code Examples
Express.js
import multer from 'multer'
const upload = multer({
limits: { fileSize: 5 * 1024 * 1024 }, // 5 MB
})
app.post('/upload', upload.single('file'), (req, res) => {
res.json({ filename: req.file?.originalname })
})
// Multer error handler
app.use((err, req, res, next) => {
if (err.code === 'LIMIT_FILE_SIZE') {
return res.status(413).json({ error: 'File too large. Maximum size is 5 MB.' })
}
next(err)
})Next.js App Router
// next.config.ts
export default {
api: { bodyParser: { sizeLimit: '4mb' } }, // pages/ router
}
// App Router route handler
export const maxRequestBodySize = '4mb' // experimental
export async function POST(request: Request) {
const formData = await request.formData()
const file = formData.get('file') as File
if (file.size > 5 * 1024 * 1024) {
return Response.json({ error: 'File too large' }, { status: 413 })
}
}Related Status Codes
Frequently Asked Questions
What does HTTP 413 Content Too Large mean?
The request body is larger than the server is willing or able to process. The server may close the connection or return a Retry-After header. Previously called 'Request Entity Too Large' in older HTTP specs.
When should an API return 413?
Typically configured at the server/proxy level (nginx client_max_body_size, Express bodyParser limit). Return 413 from application code when you enforce file size limits for uploads.
What causes an HTTP 413 error?
Common causes: File upload exceeding server limit; JSON body larger than parser limit; nginx client_max_body_size reached; Attempting to upload video to an image-only endpoint.
