Full Byte Developer.

401 vs 403 vs 404 vs 409 vs 422 vs 429: pick the right one

403 must not log your users out. Wrong status codes make clients retry forever or leak that a record exists.

Published here · 7 sources

403 must not log your users out. Wrong status codes make clients retry forever, force a re-login that will not help, or leak that a private record exists. Cheat sheet checked Nov 8 against MDN.

What the client should do

401 Unauthorized: No valid credentials. Response is sent with WWW-Authenticate [1]. Client: log in or refresh the token.

403 Forbidden: Authenticating again makes no difference [2]. Client: show no access, keep the session. Server may send 404 instead of 403 to hide that the resource exists.

409 Conflict: Conflict with current state [3]. Client: refetch, then merge. Do not auto-retry the same body.

400 Bad Request vs 422 Unprocessable Content: Broken syntax vs content the server cannot process [4][5]. Client: fix the input. Resending unchanged fails again.

429 Too Many Requests: A Retry-After header may be included [6]. Wait, then retry.

Rule of thumb

Retry 429 and 503 after Retry-After. Retry 502/504 only if the request is idempotent [7]. Never auto-retry 400, 401, 403, 409 or 422 unchanged.

Send this to the backend friend whose API logs users out on a 403.

Keep this guide

By Full Byte Developer

Filed under Dev tools

Questions about this guide

Why would a server send 404 instead of 403?

To hide that the resource exists. If you return 403, an attacker knows the record is real.

What is the difference between 400 and 422?

400 is broken syntax. 422 is valid syntax the server cannot process. Client action is the same: fix the input.

Sources

  1. MDN: 401 Unauthorizeddeveloper.mozilla.org
  2. MDN: 403 Forbiddendeveloper.mozilla.org
  3. MDN: 409 Conflictdeveloper.mozilla.org
  4. MDN: 400 Bad Requestdeveloper.mozilla.org
  5. MDN: 422 Unprocessable Contentdeveloper.mozilla.org
  6. MDN: 429 Too Many Requestsdeveloper.mozilla.org

Next in this stack