HTTP 400 Bad Request: the common causes and how to find the bad part

Open this example in the tool

Try it in the tool

HTTP Status Codes

Every HTTP status code with its RFC, meaning and when to use it.

JSON Validator

Check JSON syntax and spot duplicate keys and inexact numbers.

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 says text/plain or 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 an Authorization header with illegal characters.
  • Too large cookies or headers. nginx replies 400 Request Header Or Cookie Too Large when 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/abc where a number is expected).

Find the bad part

  1. Read the response body. A good API names the field or the position of the error. Log it.
  2. 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.
  3. 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 through JSON.stringify twice, an empty body.
  4. 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