HTTP status codes are useful only when they lead to the next sensible check. This guide turns common 2xx, 3xx, 4xx, and 5xx responses into a reusable debugging decision tree, with a curl workflow, browser inspection steps, an API error-response template, and practical guidance for retries, caching, authentication, and upstream failures.
Overview
An HTTP status code describes how a server handled a request, but it does not always identify the root cause. A 404 may indicate a wrong URL, a missing deployment route, or an intentionally hidden resource. A 502 usually points to a gateway or proxy problem, but the underlying fault may be an application crash, a service-discovery issue, or an invalid upstream response.
Start debugging by separating four questions:
- What did the client send? Check the method, URL, query parameters, headers, cookies, and request body.
- Which component returned the response? The origin application, reverse proxy, API gateway, CDN, browser cache, or another intermediary may be involved.
- Is the failure repeatable? Compare the same request in the browser, with curl, and from the relevant environment.
- What evidence identifies the request? Record the timestamp, status code, response headers, and request or trace ID without exposing credentials or personal data.
Use the status code as a routing signal rather than a complete diagnosis. The following summary is a useful first pass:
| Code or class | Meaning in practice | First check |
|---|---|---|
| 2xx | The request was handled successfully. | Confirm the response body, headers, and side effects match the contract. |
| 3xx | The client should use cached data or follow another location, depending on the response. | Inspect Location, cache headers, and redirect method behavior. |
| 4xx | The request cannot be fulfilled as sent, or the client is not permitted to proceed. | Validate URL, method, authentication, authorization, input, and rate limits. |
| 5xx | The server or an upstream dependency could not complete a valid request. | Check application logs, dependency health, timeouts, and gateway behavior. |
Checklist by scenario
When the response is 2xx
Do not stop at “the request succeeded.” For a 200 OK, verify the content type, schema, pagination fields, and any expected identifiers. A 201 Created should normally be checked for the created resource location or representation. A 204 No Content should not be parsed as JSON by a client that expects a body. If a mutation appears to succeed twice, investigate idempotency and client retries before assuming the server created duplicate data.
For frontend integration, inspect the browser Network panel and confirm that the JavaScript code handles empty responses, delayed responses, and unexpected but valid fields. A successful transport response can still expose an application-level error in the response body, so keep the API contract explicit.
When the response is 3xx
For a redirect, inspect the Location header and follow the chain one hop at a time. Confirm that the destination uses the expected host, scheme, path, and query string. Pay particular attention to authentication: a redirect from an API endpoint to a login page can look like a successful browser request while breaking a JSON client.
A 304 Not Modified is a cache validation result, not an application failure. Check the request's If-None-Match or If-Modified-Since headers and the response's cache validators. If users see stale content, compare cache directives at the browser, CDN, reverse proxy, and origin layers.
When the response is 4xx
- 400 Bad Request: Compare the actual request with the API contract. Look for malformed JSON, invalid query syntax, missing required headers, or an incorrectly encoded URL.
- 401 Unauthorized: Check whether credentials are absent, expired, malformed, or sent in the wrong authentication scheme. Avoid printing access tokens in logs. You can inspect non-secret JWT claims with a cautious JWT decoder workflow, but never paste live secrets into an untrusted tool.
- 403 Forbidden: Authentication may have succeeded, but the identity lacks permission or the request is blocked by a policy. Check scopes, roles, resource ownership, tenant boundaries, and network controls.
- 404 Not Found: Verify the base URL, route version, HTTP method, deployment environment, and trailing slash behavior. Confirm that the route exists in the deployed service rather than only in local code.
- 405 Method Not Allowed: Compare the method with the route definition and inspect the
Allowheader when provided. - 409 Conflict: Investigate state conflicts such as duplicate creation, optimistic-lock failures, or an operation that is invalid in the resource's current state.
- 422 Unprocessable Content: The request may be syntactically valid but fail validation. Return field-level details that help the caller correct the input.
- 429 Too Many Requests: Check rate-limit headers and any
Retry-Aftervalue. Use bounded exponential backoff with jitter, and do not retry every request indefinitely.
When the response is 5xx
First determine whether the problem is local to one service or shared across an edge layer. A 500 Internal Server Error calls for application logs and the request ID. A 502 Bad Gateway commonly means a gateway received an invalid response or could not communicate correctly with an upstream service; check upstream availability, connection errors, protocol mismatches, and proxy logs. A 503 Service Unavailable may indicate overload, maintenance, a failed health check, or unavailable capacity. A 504 Gateway Timeout suggests that a gateway did not receive a timely upstream response; compare timeout settings across the client, proxy, gateway, and application.
Retry only when the operation is safe to repeat or has an idempotency mechanism. For writes, an idempotency key or server-side request identity can prevent a timeout from producing an unknown duplicate operation. Treat repeated 5xx responses as an incident signal rather than a reason to increase traffic with aggressive retries.
Run the same request outside the browser
Use a minimal curl reproduction that preserves the method and relevant headers while replacing secrets with temporary or redacted values:
curl --verbose --request GET \
--url 'https://api.example.test/v1/items?limit=10' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer REDACTED'
--verbose helps reveal redirects, TLS negotiation, request headers, and response headers. Add --include when you need headers and the body together. Compare the result with the browser's Network panel, then test from the same region, container, or deployment environment if the failure may depend on network location.
What to double-check
Content negotiation and encoding
Confirm that the client sends the intended Accept and Content-Type headers. A request body encoded as form data is not interchangeable with JSON. Also check URL encoding for spaces, reserved characters, repeated parameters, and Unicode values. If a form submission fails, the HTML form debugging guide provides a useful comparison of methods and encoding types.
Headers, caching, and intermediaries
Record response headers such as Cache-Control, ETag, Location, Retry-After, and the service's request or trace identifier. A response may be generated by a cache rather than the origin. Compare headers across a cache hit, cache miss, authenticated request, and direct origin test where that test is safe and available.
Observability and correlation
Pair the client timestamp with server logs and a correlation ID. Check whether the request reached the intended release, region, tenant, and dependency version. For a timeout, distinguish connection establishment time, TLS time, time to first byte, and total response time if your tooling exposes them. This prevents a slow database query from being confused with a DNS or load-balancer problem.
Security and privacy
Redact authorization headers, cookies, API keys, personal data, and sensitive request bodies before sharing captures. Error messages should help legitimate callers correct a request without revealing stack traces, internal hostnames, credentials, or database details. When rendering error content in a web interface, follow safe output practices described in the HTML escaping and XSS prevention guide.
Use a consistent error response
An API error body should be machine-readable and useful to both callers and operators. A practical shape is:
{
"error": {
"code": "validation_failed",
"message": "One or more fields are invalid.",
"requestId": "req_redacted",
"details": [
{"field": "email", "reason": "must be a valid email address"}
],
"retryable": false
}
}
Keep the public message stable enough for users and documentation, while using the internal request ID to find detailed logs. Only include retryable when the service can define it consistently. The HTTP status remains authoritative; an application-specific error code should add context rather than replace it.
Common mistakes
- Reading only the status line: The body and headers often contain the route, validation field, retry guidance, or request ID needed for diagnosis.
- Blaming the frontend immediately: Reproduce the exact request with curl before changing UI code. The browser may also be hiding a CORS, redirect, or preflight detail.
- Treating 401 and 403 as interchangeable: One generally concerns authentication; the other generally concerns permission or policy. Check the service contract for its precise behavior.
- Retrying every failure: Retrying a malformed request will not fix it, and retrying a non-idempotent write can create duplicates. Apply bounded backoff and an operation-specific policy.
- Assuming 404 always means a missing resource: Route versions, hostnames, deployment targets, and authorization-hiding behavior can produce the same code.
- Logging secrets while debugging: Verbose request capture is valuable only when credentials and sensitive data are redacted.
- Ignoring the deployment boundary: A route can work locally but fail behind a proxy because of path rewriting, health checks, TLS termination, service discovery, or environment configuration.
When to revisit
Keep this checklist close to API integration work, deployment verification, and incident response. Revisit it before seasonal planning cycles when traffic patterns, client versions, or operational limits may change. Update your team’s version when workflows or tools change, a gateway or CDN is introduced, authentication rules are revised, or an API adds new error codes and retry behavior.
After each recurring failure, turn the diagnosis into a small regression test. Include the request method, expected status, required headers, response schema, and whether the client should retry. Test representative cases such as an expired credential, invalid input, a missing route, a rate-limited request, a slow dependency, and a successful empty response. For frontend releases, combine API checks with the cross-browser testing checklist; for page-level failures, also use the HTML validation and accessibility checklist.
A practical final sequence is: reproduce the request, capture the complete response safely, classify the status family, identify the responding layer, check the most likely cause for that code, apply the narrowest safe fix, and rerun the same request. Record the request ID and the evidence that confirmed the fix. That habit turns HTTP status codes from vague error messages into a repeatable cloud debugging workflow.