How to Fix API Request Entity Too Large (413) Error
In this tutorial, you'll learn about How to Fix API Request Entity Too Large (413) Error. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Problem
Your API client receives:
HTTP/1.1 413 Request Entity Too Large
Or:
413 Payload Too Large: The request body exceeds the server's maximum allowed size.
The request body is larger than the server is configured to accept.
Quick Fix
1. Increase the server payload limit
// Express.js — increase body parser limit
// Wrong — default 100kb
app.use(express.json())
// Right — increase to 10MB
app.use(express.json({ limit: '10mb' }))
app.use(express.urlencoded({ limit: '10mb', extended: true }))
# Flask — increase max content length
app.config['MAX_CONTENT_LENGTH'] = 10 * 1024 * 1024 # 10MB
2. Increase Nginx client_max_body_size
# Nginx — default is 1MB
http {
client_max_body_size 10M;
}
# Or per-location
location /api/upload {
client_max_body_size 50M;
proxy_pass http://backend:3000;
}
Reload Nginx:
sudo nginx -s reload
3. Use chunked or multipart uploads
Split large files into chunks for uploading:
// Client-side chunked upload
const CHUNK_SIZE = 5 * 1024 * 1024 // 5MB chunks
const file = fileInput.files[0]
const totalChunks = Math.ceil(file.size / CHUNK_SIZE)
for (let i = 0; i < totalChunks; i++) {
const chunk = file.slice(i * CHUNK_SIZE, (i + 1) * CHUNK_SIZE)
const formData = new FormData()
formData.append('chunk', chunk)
formData.append('chunkIndex', i)
formData.append('totalChunks', totalChunks)
await fetch('/api/upload/chunk', { method: 'POST', body: formData })
}
4. Compress the request body
Compress JSON or text payloads before sending:
// Client-side — compress with pako
const payload = JSON.stringify(largeData)
const compressed = pako.deflate(payload)
fetch('/api/data', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Encoding': 'deflate'
},
body: compressed
})
5. Check Apache limits
# Apache — increase limits
LimitRequestBody 10485760 # 10MB
6. Use a CDN for file uploads
Upload files directly to S3 or a CDN and send only the URL to the API:
// Upload to S3 directly
const url = await uploadToS3(file)
// Send only the URL to the API
await fetch('/api/users/avatar', {
method: 'POST',
body: JSON.stringify({ avatarUrl: url })
})
Prevention
- Set reasonable payload limits that match your use case.
- Use chunked uploads for files larger than 10MB.
- Compress API request bodies when possible.
- Use direct-to-cloud storage uploads for large files.
- Document payload size limits in your API specification.
Common Mistakes with rest too large
- Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
These mistakes appear frequently in real-world API code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro