heystack

HTTP status codes and what a program should do next

A quick reference for software calling web APIs: what each common status code means, and a sensible next step. Meanings follow RFC 9110 (HTTP Semantics) and MDN; the retry column is general guidance, not part of the standard. Whether any retry is safe depends on the request, not just the status: repeat only idempotent requests (GET, PUT, DELETE, or a POST with an idempotency key), and check whether a timed-out write already happened before repeating it.

Status codes

CodeNameWhat it meansRetry?What to do next
200OKRequest succeededn/aUse the response body
201CreatedA new resource was madeNoSave the Location header or returned ID; repeating the request would create a duplicate
202AcceptedWork queued, not finishedn/aPoll the status URL with a delay until done
204No ContentSuccess with no bodyn/aTreat as done
301Moved PermanentlyResource has a new permanent URLn/aFollow the Location URL and update the stored URL. Older clients may rewrite POST as GET; do not forward credentials or a request body to a different origin without checking it
302FoundTemporary redirectn/aFollow for GET requests. Per the Fetch standard a POST is redirected as GET, so do not rely on the method surviving; keep the original URL as the canonical one
304Not ModifiedCached copy is still currentn/aUse the cache
307Temporary RedirectTemporary redirect; method and body are preservedn/aThe same request, body included, is resent to the Location URL. Only follow if you trust that origin with the same body and credentials
308Permanent RedirectPermanent redirect; method and body are preservedn/aAs 307, but update the stored URL. Same caution about resending bodies and credentials across origins
400Bad RequestThe request is malformedNoFix the request; repeating it unchanged will fail again
401UnauthorizedMissing, expired or invalid credentialsOnceRefresh or re-check credentials once, then stop and report
403ForbiddenServer understood the request but refuses it; re-authenticating usually makes no differenceNoStop and report. A 403 does not prove the credentials are valid; the refusal is tied to permissions or application logic
404Not FoundNothing found at this URL, or the server is concealing itNoCheck the URL for typos. Note a 404 can be sent instead of 403 to avoid revealing that a resource exists
405Method Not AllowedWrong HTTP method for this URLNoRead the Allow header and the API docs
408Request TimeoutServer closed a slow or idle connectionOnly if idempotentThe request may not have been fully received; retry a safe request on a new connection
409ConflictState changed under youAfter refetchFetch the current state, merge, then resubmit
410GoneRemoved on purpose, permanentlyNoDrop the link from your records
413Content Too LargeBody exceeds the size limitNoShrink or split the body
415Unsupported Media TypeWrong Content-TypeNoSend a supported type
422Unprocessable ContentWell-formed but rejected by validationNoRead the error detail, fix the data, resubmit
429Too Many RequestsRate limit hitLaterWait for the Retry-After time if given, then slow down
500Internal Server ErrorServer-side faultOnly if idempotentRetry a few times with backoff; a failed-looking write may still have happened, so check first
502Bad GatewayAn upstream server failedOnly if idempotentRetry with backoff; often transient
503Service UnavailableOverloaded or downOnly if idempotentHonour Retry-After if present; retry with backoff
504Gateway TimeoutUpstream was too slowOnly if idempotentCheck whether the write happened before retrying

Retry rules of thumb

  • Only repeat idempotent requests, or requests carrying an idempotency key. Idempotent methods are GET, HEAD, PUT and DELETE; a plain POST is not.
  • Back off exponentially (for example 1s, 2s, 4s, 8s) and add random jitter so many clients do not retry together.
  • Cap the attempts (3 to 5) and the total wait, then report the failure instead of looping.
  • Obey a Retry-After header over your own schedule.
  • Never retry 400, 403, 404 or 422 unchanged.
  • On redirects, do not let an Authorization header or request body leak to a different origin; many HTTP clients strip credentials on cross-origin redirects for this reason.

Sources: RFC 9110 (HTTP Semantics), https://www.rfc-editor.org/rfc/rfc9110.html, and the MDN Web Docs HTTP status reference, https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status

Discussion 0

0/5,000

No discussion yet. Start it — no sign-up needed.