HTTP Status Codes / 4xx HTTP Status Codes / HTTP 401

401 Unauthorized

This is an educational reference page about HTTP 401 Unauthorized. The page itself is served as 200 OK so it can be indexed as HTTP documentation.

The request lacks valid authentication credentials for the target resource. The response must include a WWW-Authenticate challenge.

Specification

Registered in the IANA HTTP Status Code Registry as Unauthorized. Defined in RFC 9110, Section 15.5.2.

What it means

HTTP 401 Unauthorized means the request did not include valid authentication credentials. The name is misleading because 401 covers authentication, meaning who the client is. Permission checks belong to 403. RFC 9110 requires a 401 response to include a WWW-Authenticate header with at least one challenge. If the request did include credentials, 401 means the server refused those credentials.

When to use it

Common causes

Headers involved

How to fix it

Examples

Expired Bearer token

GET /api/me HTTP/1.1
Host: api.example.com
Authorization: Bearer eyJhbGciOi...

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="api", error="invalid_token", error_description="Token expired"
Content-Type: application/json

{
  "error": "invalid_token"
}

No credentials sent

GET /admin/reports HTTP/1.1
Host: example.com

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="admin", charset="UTF-8"

Common mistakes

Developer notes

The 401 vs 403 rule: 401 means the server does not know who you are, or does not accept the credentials you sent. 403 means it knows who you are and still says no. A client can fix a 401 by authenticating; a 403 needs different permissions.

Questions

What does HTTP 401 Unauthorized mean?

HTTP 401 means the request lacks valid authentication credentials. The client should authenticate, or refresh its credentials, and try again.

Is WWW-Authenticate required with 401?

Yes. RFC 9110 says a server generating a 401 response MUST send a WWW-Authenticate header with at least one challenge.

Should an expired token return 401 or 403?

Return 401. The client can fix it by getting a new token. RFC 6750 maps invalid_token, which covers expired tokens, to 401.

See also

Header and content rules

What RFC 9110 says about a 401 response, quoted from the RFC. The 4xx class rules also apply.

The same rule is in the response obligations table and its JSON.

Related comparisons

4xx client error – the request contains bad syntax or cannot be fulfilled


HTTP status code lookup
REST API status code chooser
cURL command generator
By Ping Now, a website/API/service uptime monitor
Data from Wikipedia