Software

400 Bad Request With Reproducible Examples

400 Bad Request means the server rejected the request itself. Malformed JSON, a failed validation and a bad header, sent to a test server and logged.

400 Bad Request means the server could not or would not process the request because of something in the request itself: malformed syntax, invalid JSON, a bad header, or data that fails validation. The fix is on the client side: change what is sent. Retrying the same request will fail the same way.

Five requests, followed into the logs

We sent requests to a small order API on localhost. Its application logs one line per request it handles.

1. Invalid JSON (a trailing comma):

$ curl -i -X POST http://127.0.0.1:9201/api/orders \
    -H 'Content-Type: application/json' -d '{"quantity": 2,}'
HTTP/1.1 400 Bad Request
content-type: application/json

{"error":"invalid_json","detail":"Expected double-quoted property name in JSON at position 15 (line 1 column 16)"}

[app] POST /api/orders -> 400 invalid_json

2. Valid JSON, invalid data (a string where an integer is required):

$ curl -X POST … -H 'Content-Type: application/json' -d '{"quantity": "2"}'
{"error":"validation_failed","field":"quantity","detail":"must be an integer >= 1"}

[app] POST /api/orders -> 400 validation_failed

3. Missing Content-Type (curl’s -d defaults to form encoding). Our API answers 415 here, a more specific code; many APIs return 400 instead:

{"error":"unsupported_media_type","detail":"send Content-Type: application/json"}
[app] POST /api/orders -> 415 unsupported_media_type

4. Correct request: 201 with {"id":43,"quantity":2}.

5. A broken header line, sent raw with nc:

$ printf 'GET /api/orders HTTP/1.1\r\nHost: 127.0.0.1\r\nBad Header Line\r\n\r\n' | nc 127.0.0.1 9201
HTTP/1.1 400 Bad Request
Connection: close

This one produced no application log line. Node’s HTTP parser rejected the request before our code ever saw it. That is the most important diagnostic distinction for a 400: was it rejected by the server or proxy layer (empty body, no app logs) or by the application (a descriptive body and a log line)?

DiagnoseFind out who rejected the request

Does the 400 response have a descriptive body (JSON error, field name)?

  • Yes →

    What does the error say?

    • Invalid JSON / parse error →

      Fix the JSON syntax.

      Trailing commas, single quotes, unquoted keys and comments are invalid JSON. Build the body with a JSON library instead of string concatenation, and set Content-Type: application/json.

    • A field is missing or invalid →

      Fix the data.

      Match the API's schema: types ("2" vs 2), required fields, enum values, date formats. The error usually names the field.

  • No, empty or a generic HTML page →

    Is it a browser loading a website?

    • Yes →

      Clear the site's cookies.

      Oversized or corrupted cookies make request headers too large; servers such as nginx answer 400 ('Request Header Or Cookie Too Large'). Clearing cookies for that site usually fixes it. Also check the URL for stray % characters.

    • No, an API client or script →

      Check the HTTP syntax and headers.

      Look for invalid header lines, a Content-Length that doesn't match the body, non-ASCII characters in headers, or an unencoded space in the URL. Compare with a working curl request using curl -v.

Common causes

CauseTypical signalFix
Malformed JSONparse error with a positionserialize with a JSON library
Wrong data type or missing fieldvalidation error naming the fieldfollow the API schema
Wrong or missing Content-Type400 or 415send application/json for JSON
Huge cookies or headers400 with no app log, often HTMLclear cookies; reduce header size
Invalid characters in the URL400 from the web serverpercent-encode the URL
Content-Length mismatchconnection closed, 400let the client library set it

For API developers

Return a body that says what was wrong and where (field, position), as in examples 1 and 2. Use more specific codes when they fit: 415 for the wrong media type, 413 for a payload that is too large, 422 for well-formed data that fails business rules if your API distinguishes it. Log rejected requests at the application layer, and know that parser-level rejections will only show up in server or proxy logs.

A request with a valid body but the wrong HTTP method gets a different error, reproduced in the 405 Method Not Allowed guide; one with bad credentials is covered in the 401 guide. The YAML to JSON converter is a quick way to produce valid JSON from a hand-written structure.

Sources

  1. RFC 9110: HTTP Semantics, 400 Bad Request — IETF
  2. 400 Bad Request — MDN
  3. RFC 8259: The JSON Data Interchange Format — IETF

How we made this

Reproduced in a local lab. We ran a Node.js API on localhost and sent it four POST requests with curl (invalid JSON, wrong type, missing Content-Type, valid) plus one raw request with a malformed header line via nc. Responses and the application's log lines are quoted verbatim.

Performed by Claude, an AI assistant working in the BinConvert repository (macOS, Node.js 25.8.2, curl 8.7.1, nc), November 16, 2026.

This article was drafted with AI assistance. Every example, calculation and command output on this page was re-run or checked before release; all five requests were re-sent and the responses and log lines compared. Review: automated test suite and source check, November 16, 2026.

Spotted an error? Tell us and we will correct it.

Copied to clipboard!