HTTP 401 Unauthorized: missing or invalid credentials, and 401 vs 403

Open this example in the tool

Try it in the tool

HTTP Status Codes

Every HTTP status code with its RFC, meaning and when to use it.

JWT Decoder

Decode JWTs, check expiry and verify HMAC signatures — privately.

HTTP 401 Unauthorized means the request has no valid authentication credentials for the resource. Despite the name, it is about who you are, not about what you may do: a missing token, an expired token, a wrong password. RFC 9110, section 15.5.2, requires the server to include a WWW-Authenticate header that names the scheme to use.

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="api", error="invalid_token", error_description="The access token expired"

401 or 403?

401 Unauthorized 403 Forbidden
Server’s view I do not know who you are, or I do not trust the proof I know who you are, and you are not allowed
Will the same credentials work? No, but better ones might No, only a different permission would
Client action sign in or refresh the token show an error, ask for access

If you are unsure which to send, ask: could logging in help? If yes, 401; if not, 403. The HTTP status codes tool opens both so you can read them together; the 403 page covers the other side.

Common causes

  • A malformed header. If the Authorization header is not even parsable, some servers answer 400 instead of 401.
  • No credentials were sent: the Authorization header is missing, often because a fetch call did not include it, or a redirect dropped it (clients may remove Authorization when a redirect goes to another host).
  • The wrong scheme or format: Authorization: <token> instead of Authorization: Bearer <token>, a typo in Bearer, or Basic credentials that are not Base64 of user:password.
  • An expired token. Access tokens live for minutes or hours.
  • A token from another environment: a staging token against production, or a key that was rotated.
  • A wrong audience or issuer. The token is valid, but was issued for another API.
  • Clock skew between the issuer and the verifier, so that nbf or exp fail by seconds.
  • Cookies: the session cookie expired, or was not sent because of SameSite, Secure or the domain.

How to debug a token

  1. Decode the token in the JWT decoder and read exp, nbf, iss and aud. The decoder shows the expiry as a date, so you see at once whether it has run out. Decoding does not prove the token is genuine; see JWT decoding is not verifying.
  2. Repeat the call with curl and -i: curl -i -H "Authorization: Bearer $TOKEN" https://api.example.com/me. Read the WWW-Authenticate line; a good server tells you invalid_token, expired or insufficient_scope.
  3. Compare the environments: the same call against another host, or with a token you know is fresh.
  4. Check the time on the machine that verifies: a container with a wrong clock explains random failures.

For API authors

  • Always send WWW-Authenticate and, for OAuth, the error value from RFC 6750.
  • Use one message for “unknown user” and “wrong password”, so that attackers cannot find out which accounts exist.
  • Do not use 401 for “logged in but not allowed”. That is 403.
  • For a browser app, avoid answering 401 with the Basic scheme unless you want the browser’s own sign-in dialog to appear.

Frequently asked questions

Why is it called Unauthorized when it means unauthenticated?

It is a historical naming mistake in the early specifications. The meaning in RFC 9110 is that the request lacks valid authentication credentials. Being known but not allowed is a different case, and its code is 403.

Should I retry a 401 with the same token?

No. The same credentials fail again. Refresh the token (with a refresh token or a new login) and retry once. If that fails too, send the user to sign in.

Why do I get 401 only some of the time?

Look at the token's expiry (exp) and at clock differences: a server whose clock is a minute ahead rejects a fresh token as not yet valid. Another cause is a load balancer sending requests to servers with different keys or sessions.

Does the response need a WWW-Authenticate header?

Yes, the standard requires it in a 401. It names the scheme the client should use, such as Bearer or Basic. Many APIs leave it out, and client libraries then cannot tell which login flow to start.

More in this reference