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
Authorizationheader is not even parsable, some servers answer 400 instead of 401. - No credentials were sent: the
Authorizationheader is missing, often because a fetch call did not include it, or a redirect dropped it (clients may removeAuthorizationwhen a redirect goes to another host). - The wrong scheme or format:
Authorization: <token>instead ofAuthorization: Bearer <token>, a typo inBearer, or Basic credentials that are not Base64 ofuser: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
nbforexpfail by seconds. - Cookies: the session cookie expired, or was not sent because of
SameSite,Secureor the domain.
How to debug a token
- Decode the token in the JWT decoder and read
exp,nbf,issandaud. 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. - Repeat the call with curl and
-i:curl -i -H "Authorization: Bearer $TOKEN" https://api.example.com/me. Read theWWW-Authenticateline; a good server tells youinvalid_token,expiredorinsufficient_scope. - Compare the environments: the same call against another host, or with a token you know is fresh.
- Check the time on the machine that verifies: a container with a wrong clock explains random failures.
For API authors
- Always send
WWW-Authenticateand, for OAuth, theerrorvalue 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
Basicscheme 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
- HTTP 301 Moved Permanently: redirects, caching and SEO
- HTTP 302 Found: temporary redirects, and when to use 303 or 307 instead
- HTTP 304 Not Modified: how conditional requests and ETags save bandwidth
- HTTP 400 Bad Request: the common causes and how to find the bad part
- HTTP 403 Forbidden: why access is refused and how to fix it
- HTTP 404 Not Found: causes, how to debug it and what to return instead
- HTTP 429 Too Many Requests: what it means and how to handle it
- HTTP 500 Internal Server Error: how to find the cause and what to tell users
- HTTP 502 Bad Gateway: finding the broken link between the proxy and the app
- HTTP 503 Service Unavailable: maintenance pages, overload and Retry-After
- HTTP 504 Gateway Timeout: why the proxy gave up waiting, and the timeouts to check