Maaz Zindani

Loading the index…

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

A Node.js server answers 431 Request Header Fields Too Large

ErrorFailed to load resource: the server responded with a status of 431 (Request Header Fields Too Large)

Short answer

431 means the server refused the request because its headers were too large. Node.js answers 431 when the request headers go past its limit, 16 KiB by default. Measure the headers, remove what is oversized (usually cookies or a long token), and only then raise the limit with --max-http-header-size.

  • Applies to Node.js 22, curl 8
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Repeat the failing request with curl and print the size of what you send. Compare it with the 16 KiB limit.

    curl -sI -H "Cookie: <paste the Cookie header value from the browser>" http://localhost:3000/ | head -n 1
  2. 2Match what you see, then jump to its fix

    1. 1The 431 appears for one browser or one signed-in user, and a private window worksOne header, usually cookies or a token, is oversizedFix
    2. 2The request works against other servers but this Node.js process returns 431The headers are legitimate but over Node.js's size limitFix

What does the error look like?

  • Failed to load resource: the server responded with a status of 431 (Request Header Fields Too Large)
  • HTTP/1.1 431 Request Header Fields Too Large

Cause 1 of 2

One header, usually cookies or a token, is oversized

1Check

The standard says the client may send the request again after making its header fields smaller. Open the browser's network tab and look at the Cookie and Authorization headers on the failing request.

printf '%s' "<paste the Cookie header value>" | wc -c

2Fix

Stop storing large data in cookies and tokens, delete the stale cookies for the host and sign in again.

// store a short id in the cookie, keep the data on the server
res.cookie('sid', sessionId, { httpOnly: true });

3Verify

Repeat the request; the status is 200 instead of 431.

curl -I http://localhost:3000/

Confirmed by rfc-editor.org

Did this fix it?

Cause 2 of 2

The headers are legitimate but over Node.js's size limit

1Check

Node.js rejects requests whose headers exceed its limit, which is 16 KiB by default and can be changed with --max-http-header-size. Check how the process is started in your scripts or process manager.

grep -rn max-http-header-size package.json

2Fix

Start the server with a larger limit in bytes. Treat this as a stopgap and still reduce the header size.

node --max-http-header-size=32768 server.js

3Verify

The same request now gets a normal response.

curl -I http://localhost:3000/

Confirmed by nodejs.org

Did this fix it?

How do I stop it coming back?

  • Keep cookies and bearer tokens small; keep session data on the server.
  • Clear cookies for localhost between projects, since the browser sends every cookie for the host to every app on it.
  • Do not raise the header limit globally without a reason; large limits make abuse easier.

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

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

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