tools / HTTP reference / http 401 vs 403: which one to return
HTTP · RFC 7235 §3.1 (401), RFC 7231 §6.5.3 (403)

401 Unauthorized vs 403 Forbidden

401 = 'I don't know who you are' (missing or invalid credentials). 403 = 'I know who you are, you can't have this'. Misusing them confuses clients and breaks browser auth flows.

intermediate RFC 7235 §3.1 (401), RFC 7231 §6.5.3 (403)

What the RFC says

401 Unauthorized: The server generated this response when the request requires user authentication... A user agent that wishes to authenticate will usually do so by including an Authorization header field containing the credentials. (RFC 7235 §3.1) — RFC 7235 §3.1 (401), RFC 7231 §6.5.3 (403)

Example

GET /api/users HTTP/1.1
Authorization: (none)

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

# vs.

GET /api/admin HTTP/1.1
Authorization: Bearer <valid token, but role=user>

HTTP/1.1 403 Forbidden

Real-world implementations

Common misuses (don't do this)

Use cases

Get the RFC reference for any HTTP response
httpwut takes a curl response and explains the status code + headers + the RFC sections you should actually read. Built for HTTP debugging that goes deeper than 'lol 500'.
Open httpwut  

Related HTTP topics

422 unprocessable entity (validation errors) · 422 unprocessable entity (validation errors) · 422 unprocessable entity (validation errors) · 422 unprocessable entity (validation errors)