400 Bad Request

The server refused to process the request because something about the request itself looks wrong. Sending the same bytes again will get the same answer.

What 400 means

A 400 says the fault is in what the client sent, not in what it asked for. The specification gives three examples: malformed syntax, invalid message framing (a body whose length does not match its headers, say) and deceptive routing. In everyday use it has grown into the general "your request is wrong" code, used whenever nothing more specific fits.

Two very different things send it, and the first job is to tell them apart:

  • The web server or proxy rejects the request before any application code runs. The body is a short stock HTML page.
  • The application parsed the HTTP fine and objects to the content: invalid JSON, a missing parameter, a value of the wrong type. A well-built API explains itself in the body.

400 in a browser

When an ordinary page load returns 400, the usual culprit is the headers your browser sends for that site, cookies above all.

  • "Request Header Or Cookie Too Large" is nginx's wording. A request header line that does not fit in one of its large_client_header_buffers (four buffers of 8k by default) is rejected with 400. Apache applies LimitRequestFieldSize, 8190 bytes by default. Delete the cookies for that site and reload.
  • "The plain HTTP request was sent to HTTPS port", again from nginx: the URL says http:// but the port expects TLS. Change the scheme to https://.
  • A hand-edited or badly copied URL with a stray % or other characters the server will not accept.

400 from an API

  • Invalid JSON. A trailing comma, single quotes, an unescaped line break inside a string, or a body cut short. Paste the exact body into the JSON formatter to find the position of the error.
  • Body and Content-Type disagree. Form-encoded data sent as application/json, or the reverse. Some APIs answer 415 for this; many just say 400.
  • Missing or mistyped parameters. A required field left out, a string where a number is expected, a date in the wrong format.
  • A URL that was not encoded. Spaces, & or # inside a query value change how the URL is split. The URL parser shows what the server actually receives.
  • No Host header. HTTP/1.1 requires one, and servers answer 400 without it. This only happens with hand-written requests and very old clients.

Read the response body before anything else. Then log the request as sent, with headers, and compare it with the API documentation. Do not retry automatically; nothing will change until the request does.

Sending 400 from your own server

  • Say what is wrong and where: the field name, the expected format, the position of a JSON syntax error. A bare "Bad Request" costs every integrator an hour.
  • Reach for a more specific code when one fits: 401 for missing credentials, 404 for an unknown ID in the path, 413 for a body over the limit, 415 for an unsupported format.
  • Decide once how you split 400 and 422 and keep to it across the API.
  • Never answer 500 for input you failed to validate. An exception thrown by a JSON parser is the client's error and should surface as 400.

400 compared with 422, 415 and 404

CodeThe requestExample
400 Bad RequestCannot be parsed, or is wrong in a way no other code covers{"qty": 2,}
422 Unprocessable ContentParses correctly but breaks a rule{"qty": -2}
415 Unsupported Media TypeIs in a format this endpoint does not acceptXML sent to a JSON-only endpoint
404 Not FoundIs fine, but points at nothing/orders/8123 when there is no such order

Many APIs use 400 for every row except the last, and that is acceptable. What matters is that the body tells the client which case it hit.

Example 400 response

HTTP/1.1 400 Bad Request
Content-Type: application/json
Content-Length: 76

{"error":"invalid_json","message":"Unexpected token } at line 1, column 11"}

The message gives the position of the syntax error, so the caller does not have to guess which part of the body is at fault.

Defined in RFC 9110, section 15.5.1.

Questions

How do I fix a 400 Bad Request in Chrome?

Check the address for stray characters first. If it looks right, delete the cookies for that one site (the padlock or settings icon in the address bar, then Cookies and site data) and reload. An oversized or corrupted cookie is the most common cause of a 400 on a normal page.

Is a 400 error the client's fault?

Formally yes: the 4xx class means the request was wrong. In practice a server can also be too strict or misconfigured, for example a header size limit that is smaller than the cookies the same site sets.

Should an API return 400 or 422 for validation errors?

Either is defensible. A common split is 400 for requests that cannot be parsed and 422 for requests that parse but contain invalid values. Pick one rule, document it and return details in the body.

Why does my request work in Postman but return 400 from code?

The two are not sending the same bytes. Compare the Content-Type header, how the body is serialised (a JSON string against form fields) and how the URL is encoded. Log the raw request from your code and put it next to the one Postman generates.