Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Express rejects a request body as too large

ErrorHTTP 413 returned by Express when a JSON or form body is larger than the limit option

Short answer

A 413 means a body was bigger than a size limit. In Express, express.json and express.urlencoded accept 100kb by default, so raise the limit option on the parser that handles that content type. If the error page is from nginx, not Express, raise client_max_body_size instead.

  • Applies to Node.js 22, Express 5, nginx 1.26
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find out who answered. Express returns the error from its error handler, while nginx returns its own HTML page. The Server header and the body tell them apart.

    curl -i -X POST http://localhost:3000/api/upload \
      -H "Content-Type: application/json" \
      --data-binary @big.json | head -n 15
  2. 2Match what you see, then jump to its fix

    1. 1413 with request entity too large text from Express, for JSON or form posts bigger than 100kbexpress.json or express.urlencoded still has the default limitFix
    2. 2You raised a limit but the error stays; two parsers are mounted for the same content typeA second parser is registered earlier with the default limitFix
    3. 3The response is an nginx HTML page saying 413 Request Entity Too Large, and Express logs nothingnginx in front of the app has its own 1 MB body limitFix

What does the error look like?

  • HTTP 413 returned by Express when a JSON or form body is larger than the limit option
  • 413 (Request Entity Too Large) returned by nginx in front of the app

Cause 1 of 3

express.json or express.urlencoded still has the default limit

1Check

Both parsers have a limit option that defaults to 100kb. Large JSON bodies, base64 images in JSON and big forms are the usual cases.

grep -rn "express.json\|express.urlencoded\|bodyParser" --include=*.js .

2Fix

Set limit to the largest body you really want to accept, not to a huge number.

app.use(express.json({ limit: '5mb' }));
app.use(express.urlencoded({ extended: true, limit: '5mb' }));

3Verify

Post the same body again; the status is no longer 413.

curl -s -o /dev/null -w "%{http_code}\n" -X POST http://localhost:3000/api/upload \
  -H "Content-Type: application/json" --data-binary @big.json

Confirmed by expressjs.com

Did this fix it?

Cause 2 of 3

A second parser is registered earlier with the default limit

1Check

Each parser only handles requests whose Content-Type matches its type option, and the first one that matches reads the body. If a default-limit express.json comes first, your larger one never sees the request.

grep -rn "express.json\|express.urlencoded" --include=*.js . | sort

2Fix

Keep one parser per content type and give it the limit. For a single heavy route, mount a larger parser on that route only, before the global one is reached.

const bigJson = express.json({ limit: '20mb' });
app.post('/api/import', bigJson, importHandler);
app.use(express.json());

3Verify

Post a large body to the route that needs it and a large one to another route; only the first is accepted.

curl -s -o /dev/null -w "%{http_code}\n" -X POST http://localhost:3000/api/import \
  -H "Content-Type: application/json" --data-binary @big.json

Confirmed by expressjs.com

Did this fix it?

Cause 3 of 3

nginx in front of the app has its own 1 MB body limit

1Check

nginx refuses bodies above client_max_body_size, which is 1m unless you changed it. The request never reaches Node.js.

sudo nginx -T 2>/dev/null | grep -n client_max_body_size

2Fix

Set a limit in the server or location block that matches what Express accepts, then reload.

server {
    client_max_body_size 5m;
}

3Verify

Test the config, reload, and post the large body through the public address.

sudo nginx -t && sudo systemctl reload nginx
curl -s -o /dev/null -w "%{http_code}\n" -X POST https://example.com/api/upload \
  -H "Content-Type: application/json" --data-binary @big.json

Confirmed by nginx.org

Did this fix it?

How do I stop it coming back?

  • Set limits on purpose: small on public JSON routes, larger only on the upload routes that need it.
  • Send files as multipart or direct-to-storage uploads, not as base64 inside JSON.
  • Keep the Express limit and the nginx client_max_body_size in the same file of notes so they change together.
  • Return a clear message from your error handler when the status is 413.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.