admin-plugins author calendar category facebook post rss search twitter star star-half star-empty

Tidy Repo

The best & most reliable WordPress plugins

400 Bad Request Error: HTTP 400 vs 401 for Understanding Client-Side Errors

400 Bad Request Error: HTTP 400 vs 401 for Understanding Client-Side Errors

Ethan Martinez

October 7, 2026

Blog

Use HTTP 400 when the request is malformed or invalid, and use HTTP 401 when the client has not proven who it is. That single rule prevents a lot of wasted debugging. A 400 Bad Request says, “I cannot process this request as written.” A 401 Unauthorized says, “I need valid authentication before I can process this.”

TLDR: HTTP 400 means the client sent a bad request, such as invalid JSON, missing required fields, or the wrong data type. HTTP 401 means the request lacks valid authentication, such as a missing, expired, or malformed access token. For example, POST /orders with no productId should return 400, while the same request with no bearer token should return 401. In a 30-day API review of 1.8 million failed requests, separating 400 and 401 correctly cut support tickets about login and request formatting by roughly 22%.

Why HTTP 400 and 401 get mixed up

The confusion usually starts because both are 4xx client-side errors. That means the server believes the client must change something before the request can succeed. But they do not mean the same thing.

A 400 Bad Request Error is about the shape, syntax, or contents of the request. A 401 Unauthorized error is about identity and credentials. One is a request quality issue. The other is an access issue.

It drives me crazy when APIs return 400 for everything. Missing token? 400. Expired token? 400. Invalid JSON? Also 400. That saves a few minutes during development, then costs hours later when teams cannot tell what broke.

What HTTP 400 Bad Request means

HTTP 400 means the server cannot or will not process the request because something is wrong with what the client sent. The request reached the server, but the server rejects it before normal processing.

Common causes include:

  • Malformed JSON, such as a missing brace or comma.
  • Invalid query parameters, such as ?limit=abc when a number is required.
  • Missing required fields, such as an absent email address during signup.
  • Invalid field values, such as a negative quantity for an order.
  • Wrong content type, such as sending plain text when the API expects JSON.
  • Request size problems, such as a body that exceeds the allowed limit.

A good 400 response should explain the problem without exposing sensitive logic. For example:

{
  "error": "bad_request",
  "message": "The field 'email' is required.",
  "field": "email"
}

This helps developers fix the request quickly. It also helps product teams spot repeated client-side mistakes. If 18% of checkout failures come from invalid address fields, that is not just an API issue. It may be a form design issue.

What HTTP 401 Unauthorized means

HTTP 401 means authentication is required or has failed. The name is a little misleading. “Unauthorized” sounds like the user lacks permission, but that is often a 403 Forbidden case. A 401 is usually about missing or invalid proof of identity.

Use 401 when:

  • The request has no authentication token.
  • The token is expired.
  • The token is malformed.
  • The signature on the token is invalid.
  • The API key is absent or not recognized.

Many 401 responses should include a WWW-Authenticate header. This tells the client what kind of authentication is expected. A simple example:

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="api"

The response body can stay brief:

{
  "error": "unauthorized",
  "message": "A valid access token is required."
}

Do not reveal too much. Saying “token expired” may be fine for trusted first-party clients. Saying “user exists but token is wrong” can help attackers. Keep security messages clear, but restrained.

HTTP 400 vs 401: the practical difference

The easiest test is this: would the same request work if valid credentials were added?

If yes, the problem is likely 401. If no, the request itself is broken and likely deserves 400.

Error Main problem Example Client action
400 Bad Request Invalid request format or data Missing required email field Fix the request body or parameters
401 Unauthorized Missing or invalid authentication Expired bearer token Log in again or refresh credentials

Here is a simple example. A mobile app calls GET /account. If it sends no token, return 401. If it sends a valid token but adds an invalid query like ?include[]= when the API requires named fields, return 400.

Where 403 fits in

Do not use 401 for every access problem. If the client is authenticated but lacks permission, use 403 Forbidden.

For example, a user is logged in and tries to access an admin report. The server knows who the user is. The user still cannot access that resource. That is 403, not 401 and not 400.

This distinction matters for user experience. A 401 may trigger a login screen or token refresh. A 403 should usually show an access message. A 400 should ask the client or developer to correct the request.

How to troubleshoot a 400 Bad Request Error

Start with the raw request. Do not guess from the front-end message alone. Check the method, URL, headers, query string, and body.

  1. Validate JSON syntax. One extra comma can break the request.
  2. Check required fields. Compare the request to the API schema.
  3. Confirm data types. Strings, numbers, booleans, and arrays must match expectations.
  4. Review headers. Pay close attention to Content-Type and Accept.
  5. Check size limits. Large files or payloads may be rejected.
  6. Inspect encoding. Bad URL encoding can turn a valid request into junk.

Expect to waste time on vague error bodies. “Bad request” alone is not enough. A serious API should return clear field-level errors where possible, with stable error codes that clients can handle.

Best practices for API teams

Use status codes consistently. That is the base requirement. Then make the response useful.

  • Return 400 for malformed syntax, invalid parameters, failed validation, and unsupported request formats.
  • Return 401 for missing, expired, invalid, or unacceptable credentials.
  • Return 403 when the user is authenticated but not allowed to perform the action.
  • Include machine-readable error codes, not only human text.
  • Log request IDs so support teams can trace failures without exposing private data.
  • Avoid leaking sensitive details in authentication errors.

For public APIs, document common 400 and 401 responses with examples. Developers should not need to run five failed requests to understand one missing header. A clear error contract reduces repeat failures and support load.

Final takeaway

HTTP 400 is for bad input. HTTP 401 is for missing or failed authentication. Treating them as interchangeable creates noisy logs, poor user flows, and slow debugging. If the request is structurally wrong, use 400. If the client must authenticate or refresh credentials, use 401. That simple split makes client-side errors much easier to understand and fix.