error http

HTTP 413 Payload Too Large

Understanding HTTP 413 Payload Too Large - the request body exceeds the maximum size the server is willing to process.

What It Means

HTTP 413 Payload Too Large (formerly “Request Entity Too Large”) indicates that the server is refusing to process a request because the request body is larger than the server is willing or able to process. The server may close the connection to prevent the client from continuing the request.

Common Causes

  • Uploading a file that exceeds the server’s maximum upload size
  • Sending a large JSON payload that exceeds body parser limits
  • Nginx or reverse proxy client_max_body_size too small
  • Express.js body parser limit too restrictive
  • Base64-encoded images in JSON payloads inflating body size
  • Batch API requests with too many items

How to Fix

Nginx - Increase upload limit

# nginx.conf or site config
http {
    client_max_body_size 50M;  # Default is 1M
}

# Or per-location
location /api/upload {
    client_max_body_size 100M;
}

Express.js - Increase body parser limit

const express = require('express');
const app = express();

// Increase JSON body limit (default is 100kb)
app.use(express.json({ limit: '10mb' }));

// Increase URL-encoded body limit
app.use(express.urlencoded({ limit: '10mb', extended: true }));

// For file uploads with multer
const multer = require('multer');
const upload = multer({
  limits: {
    fileSize: 50 * 1024 * 1024, // 50 MB
  }
});

app.post('/api/upload', upload.single('file'), (req, res) => {
  res.json({ filename: req.file.filename });
});

Apache - Increase limit

# .htaccess or httpd.conf
LimitRequestBody 52428800  # 50 MB in bytes

# Also update PHP settings if applicable
php_value upload_max_filesize 50M
php_value post_max_size 55M

Client-side: Validate file size before uploading

const MAX_FILE_SIZE = 50 * 1024 * 1024; // 50 MB

function handleFileUpload(event) {
  const file = event.target.files[0];

  if (file.size > MAX_FILE_SIZE) {
    alert(`File is too large. Maximum size is ${MAX_FILE_SIZE / 1024 / 1024} MB.`);
    return;
  }

  const formData = new FormData();
  formData.append('file', file);

  fetch('/api/upload', { method: 'POST', body: formData });
}

Chunked uploads for large files

async function uploadInChunks(file, chunkSize = 5 * 1024 * 1024) {
  const totalChunks = Math.ceil(file.size / chunkSize);

  for (let i = 0; i < totalChunks; i++) {
    const start = i * chunkSize;
    const end = Math.min(start + chunkSize, file.size);
    const chunk = file.slice(start, end);

    await fetch('/api/upload/chunk', {
      method: 'POST',
      headers: {
        'X-Chunk-Index': i,
        'X-Total-Chunks': totalChunks,
      },
      body: chunk
    });
  }
}
  • HTTP 400 - Bad Request: The request is malformed, not just too large.
  • HTTP 415 - Unsupported Media Type: The format of the payload is not supported.
  • HTTP 408 - Request Timeout: Large uploads may also trigger timeouts on slow connections.