HTTP 400 Bad Request means the server cannot or will not process the request because it appears to be malformed. It is defined in RFC 9110, section 15.5.1. It is a client error: sending the same request again gives the same answer, so the request must change.
What usually causes a 400
- Invalid JSON in the body: a trailing comma, single quotes, a comment, an unescaped newline in a string, or a body that was cut off. Paste the exact body into the JSON validator, which points at the first problem.
- A wrong or missing
Content-Type. The body is JSON but the header saystext/plainor a form type, and the server parses it the wrong way. (Some servers answer 415 here instead.) - A bad URL: a
%that is not followed by two hex digits, a space in the path, or an invalid character in the query string. The URL encoder shows the correct form of a value. - Header problems: a malformed header line, a duplicate
Host, or anAuthorizationheader with illegal characters. - Too large cookies or headers. nginx replies
400 Request Header Or Cookie Too Largewhen they exceed its buffers. - A request framing problem: a wrong
Content-Length, or a chunked body that does not follow the rules. This often comes from a proxy or a hand-written client. - Validation done by the framework: many frameworks return 400 when a path or query parameter has the wrong type (
/users/abcwhere a number is expected).
Find the bad part
- Read the response body. A good API names the field or the position of the error. Log it.
- Reproduce with curl:
curl -i -X POST https://api.example.com/items -H 'Content-Type: application/json' -d @body.json. A request that works in curl and fails in your code points at how the code builds the body or headers. - Look at what was really sent, not at what you meant to send: use the Network panel, or the proxy’s log. Typical surprises:
[object Object]where JSON should be, a body that went throughJSON.stringifytwice, an empty body. - Shrink the request until it works, then add parts back one by one.
400, 422 or 415?
| Problem | Best status |
|---|---|
| Body cannot be parsed (invalid JSON) | 400 |
| Body parses, but a value is not acceptable | 422 |
| The format of the body is not supported | 415 |
Required condition header is missing (If-Match) |
428 |
| Headers are too large | 431 |
The HTTP status codes tool lets you open 400, 422 together to compare the wording and the RFC sections.
For API authors
Return a body with a stable error code, a human message and, for validation, the list of fields. Keep the shape the same for all errors so clients can handle it in one place. Do not use 400 for authentication or permission problems: those are 401 and 403. Do not return 200 with an error in the body.
Frequently asked questions
What is the difference between 400 and 422?
400 means the request could not be parsed: broken JSON, bad framing, an invalid header. 422 means it was parsed and understood, but the values are not acceptable, for example a missing required field or an age of -5. Many APIs use 400 for both, which is allowed, but 422 tells a client more.
Why does a website show 400 'Request Header Or Cookie Too Large'?
The browser sent more cookie data than the server accepts. nginx answers 400 for this case, and the standard code for it is 431. Clearing the cookies of that site fixes it for the visitor, and the real fix is to store less in cookies.
Can I fix a 400 from the browser?
Sometimes. Reload the page, clear the site's cookies, and check that the address has no stray characters or unencoded spaces. If the page still fails, the problem is usually in the site's own code.
Should a server explain a 400?
Yes. Say what was wrong and where, for example the field name or the position in the JSON. A bare 'Bad Request' sends the developer on a long hunt. Do not echo back secrets or a full stack trace.
More in this reference
- HTTP 301 Moved Permanently: redirects, caching and SEO
- HTTP 302 Found: temporary redirects, and when to use 303 or 307 instead
- HTTP 304 Not Modified: how conditional requests and ETags save bandwidth
- HTTP 401 Unauthorized: missing or invalid credentials, and 401 vs 403
- HTTP 403 Forbidden: why access is refused and how to fix it
- HTTP 404 Not Found: causes, how to debug it and what to return instead
- HTTP 429 Too Many Requests: what it means and how to handle it
- HTTP 500 Internal Server Error: how to find the cause and what to tell users
- HTTP 502 Bad Gateway: finding the broken link between the proxy and the app
- HTTP 503 Service Unavailable: maintenance pages, overload and Retry-After
- HTTP 504 Gateway Timeout: why the proxy gave up waiting, and the timeouts to check