401 Unauthorized

The request was not applied because it has no valid authentication credentials. The server does not know who you are, or does not accept the proof you sent.

What 401 means

The name is misleading. A 401 is about authentication (who are you?), not authorisation (are you allowed?). It covers a request with no credentials and a request whose credentials were rejected: an expired token, a wrong password, a revoked key.

It is also an invitation to try again. A client that gets a 401 may repeat the request with new credentials, which is exactly what a browser does when it shows a login box. If the server knows who you are and the answer is still no, the right code is 403.

The WWW-Authenticate header

A server that sends 401 must include a WWW-Authenticate header with at least one challenge, naming the scheme it accepts:

WWW-Authenticate: Basic realm="Staging"
WWW-Authenticate: Bearer realm="api", error="invalid_token"

With Basic, a browser prompts for a user name and password. With Bearer it shows nothing; the challenge is for API clients, and its error parameter tells them whether the token was missing, malformed or expired.

Plenty of APIs leave the header out. That breaks the specification, and some HTTP libraries behave oddly without it. Others send 403 for unauthenticated requests precisely so that browsers do not pop up the Basic dialog. Neither is correct, and you will meet both.

Debugging a 401 from an API

  • Is the header really being sent? Log the outgoing request. Browsers, curl and most HTTP libraries drop Authorization when a redirect leads to a different host, so a 301 from example.com to www.example.com silently strips your credentials. Call the final URL directly.
  • Is the format exact? Authorization: Bearer <token> with one space, no quotes, and no line break at the end of a token read from a file.
  • Has the token expired? Paste a JWT into the JWT decoder and read exp, which is a Unix timestamp in seconds. Check aud and iss while you are there: a token issued for staging will not pass in production.
  • Does the server's clock agree? A machine that is minutes out rejects tokens as expired or not yet valid.
  • Is a proxy removing the header? Under Apache, CGI and FastCGI applications do not receive Authorization unless CGIPassAuth On is set.

Handle 401 in code by refreshing the token once and repeating the request. If the second attempt fails too, stop and ask the user to sign in. A loop of refreshes helps nobody.

Protecting a site with HTTP authentication

  • nginx: auth_basic "Staging"; and auth_basic_user_file /etc/nginx/.htpasswd; in the server or location block.
  • Apache: AuthType Basic, AuthName "Staging", AuthUserFile /etc/apache2/.htpasswd and Require valid-user.

Both send the 401 and the Basic challenge for you. Use it only over HTTPS: Basic credentials are base64-encoded, not encrypted.

401 compared with 403, 404 and 407

CodeSituationWill new credentials help?
401 UnauthorizedNo credentials, or credentials that were not acceptedYes
403 ForbiddenIdentity is known, or irrelevant; access is refusedOnly credentials for a different account
404 Not FoundThe server is hiding that the resource existsUnknown, by design
407 Proxy Authentication RequiredA proxy between you and the server wants credentialsYes, in Proxy-Authorization

Example 401 response

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="api", error="invalid_token", error_description="The access token expired"
Content-Type: application/json
Content-Length: 62

{"error":"invalid_token","message":"The access token expired"}

The challenge tells a client both how to authenticate (Bearer) and why this attempt failed. On invalid_token a client should fetch a new token and try once more.

Defined in RFC 9110, section 15.5.2.

Questions

What is the difference between 401 and 403?

A 401 means the server could not authenticate you: credentials are missing or invalid, and sending valid ones may work. A 403 means the server refuses the request anyway; logging in again as the same user will not change the answer.

Why is it called Unauthorized if it means unauthenticated?

The name dates from the earliest HTTP specifications and has been kept for compatibility. The definition has always been about authentication: the response has to carry a WWW-Authenticate challenge, which only makes sense when the server is asking who you are.

Why do I get 401 when my token is correct?

Usually the token never arrives or is no longer valid: the Authorization header was dropped on a redirect to another host, the token expired, it was issued for a different environment or audience, or it has a stray space or line break around it.

Does a 401 response need a WWW-Authenticate header?

Yes. The HTTP specification requires at least one challenge in every 401 response. Many APIs omit it and most clients cope, but including it is correct and lets generic HTTP libraries react properly.