Skip to content

Troubleshooting guide

400 Bad Request: what it means and how to fix it

A 400 means a server decided the request itself was broken and refused to process it. The useful question is which layer said so: the CDN, the web server, or your application.

4 min read10 sectionsUpdated

On this page

Key points

  • 400 is a client-side verdict, but your own front end or proxy may be the client that built the bad request.
  • Cookies are the top cause in browsers: one oversized or corrupted cookie breaks every page on a domain.
  • In APIs it is usually validation: malformed JSON, a wrong content type or a missing field.
  • The response body and Server header tell you which layer rejected it.

What 400 means

The server received the request and could not or would not process it because something about the request is wrong. Unlike a 500, retrying the identical request will fail the same way. Unlike a 404, the server never got far enough to look for the resource.

Web servers return 400 for protocol-level problems, such as a malformed request line or a header they cannot parse. Applications return 400 for business-level validation, such as a date in the wrong format. Those are very different problems that share one number, so start by finding out who answered.

Common causes

  • Corrupted or oversized cookies. A session cookie that grew too large, or one left over from an old deployment, fails on every request to that domain.
  • Malformed URLs. An unencoded space, a stray % or an invalid character in the path or query string.
  • Invalid JSON or the wrong Content-Type. The body says one thing and the header another, or a trailing comma broke the parser.
  • Plain HTTP sent to an HTTPS port. Nginx answers this with its own 400 page.
  • A WAF or proxy rule rejecting a request it considers suspicious.

Find which layer rejected it

Reproduce the request outside the browser and read the headers. A Server: cloudflare header with a CDN-branded body means it never reached you; a JSON error body with your own format means your application refused it.

bash
# See status, server and body
curl -si https://example.com/path | head -20

# Same request without any cookies: if this works, a cookie is the cause
curl -s -o /dev/null -w "%{http_code}\n" https://example.com/path

# Reproduce an API call exactly, with the content type
curl -si -X POST https://api.example.com/items \
  -H 'Content-Type: application/json' -d '{"name":"test"}'

# Nginx logs malformed requests here, with the reason
sudo tail -f /var/log/nginx/error.log

Fixes

As a visitor: clear cookies for that one site, or try a private window. If the private window works, a cookie was the cause.

As a site owner: keep cookies small and scoped to the paths that need them, encode user input before putting it into URLs, and raise large_client_header_buffers in Nginx only if legitimate headers really are large. In APIs, return a body naming the field that failed, so a 400 can be fixed by reading it rather than by searching logs.

Catch it automatically

A sudden rise in 400s after a deployment nearly always means a client and server disagree about a contract. Track the 4xx rate per endpoint in APM and alert on change rather than on an absolute number, because some steady level of 400s from bots is normal. Log the rejected field or reason in your structured logs, and never the full request body, which can contain secrets.

Frequently asked questions

Is a 400 error my fault or the website's?

It depends on the layer. If it happens only on one site and clearing its cookies fixes it, the problem was stored in your browser. If every visitor gets it, the site is building or validating requests incorrectly.

Why do I get 400 only when logged in?

Logging in usually adds cookies. A session or identity cookie that has grown past the server's header limit produces a 400 exactly then, and the error disappears when you log out.

Should an API use 400 or 422?

Either is acceptable. A common convention is 400 for a body that cannot be parsed and 422 for one that parses but fails validation. Consistency matters more than the choice.

Check it now, free

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.