HTTP Client Error Status Codes (4xx)

Guide to 4xx client error responses: when to use 400 vs 401 vs 403 vs 404 vs 429, proper error responses, and user experience.

The data

Client errors

CodeNameCauseExampleResponse bodyNotesResponse headers
400Bad RequestMalformed syntax, invalid request message framing, or deceptive request routing.Missing required JSON field, invalid JSON syntax, query parameter type mismatch{"error": "Invalid input", "field": "email", "message": "Email format invalid"}Server cannot or will not process request due to client error (not server fault)null
401UnauthorizedRequest requires user authentication; credentials missing or invalid.No Authorization header, expired JWT, invalid API keynullDo not use for missing permissions (use 403); client may retry with credentialsWWW-Authenticate: Bearer realm="api"
403ForbiddenServer understood request but refuses to authorize; valid credentials but insufficient permissions.User role lacks access to admin endpoint, IP blocked, account suspended{"error": "Access denied", "reason": "insufficient_privileges"}Never expose why access denied in production (security through obscurity); log details server-sidenull
404Not FoundRequested resource does not exist on server.Non-existent URL path, deleted resource, typo in endpointnullDo not reveal whether resource exists (prevents user enumeration); return generic messagenull
405Method Not AllowedRequest method not supported for target resource.GET on /api/users (expects POST), DELETE on read-only endpointnullInclude Allow header listing supported methodsAllow: POST, PUT
408Request TimeoutServer did not receive complete request in time it was prepared to wait.Client slow-down; slow POST body upload, client closed connection prematurelynullMay be retried by client with exponential backoff; distinguish from 504 (server timeout)null
409ConflictRequest conflicts with current state of resource; cannot be completed.Editing stale version (optimistic concurrency), duplicate unique key, file lock conflict{"error": "Conflict", "detail": "Version mismatch; resource modified by others"}Include details about conflict in body so client can resolve (e.g., current version)null
429Too Many RequestsUser sent too many requests in given time (rate limiting).Exceeded 100 requests/minute limit, burst limit exceedednullInclude Retry-After header; implement exponential backoff on clientRetry-After: 60 (seconds)
431Request Header Fields Too LargeServer unwilling to process request due to individual header fields too large.Cookie header exceeds 4KB, extremely long User-Agent, huge custom headernullOften due to oversized cookies; consider clearing cookies or session resetnull

Error response format

Standard fields
  • error
  • message
  • details
  • code
  • timestamp
Example
Error
invalid_input
Message
The provided email address is not valid.
Field
email
Value
not-an-email
Documentation url
https://api.example.com/docs/errors#INVALID_EMAIL
Security notes
Never leak internal error messages, stack traces, SQL, file paths, or server configuration in production errors

Fetch the same bytes

The static files are identical to what the API returns, but with no rate limit and no server round trip. Use the API when you want a query and a content type; use the files when you want to cache one document.

curl "https://yjtoon.com/api/dataset/http-client-error-guide?format=toon"
const res = await fetch(
  "https://yjtoon.com/static-data/dataset/http-client-error-guide.toon"
);
const toon = await res.text();

Rate limit: 120 requests per minute per IP, no key and no signup. API reference →

Topics

  • http
  • client-errors
  • 4xx
  • error-handling
  • security
  • rate-limiting