401 Unauthorized means: The request lacks valid authentication credentials for the target resource. Despite the name, it means 'unauthenticated', not 'unauthorized': the server must include a WWW-Authenticate header describing how to authenticate, and the client may retry with credentials. Client-error 4xx codes mean the request itself is the problem: malformed syntax, missing credentials, forbidden access, a nonexistent resource or a violated precondition. The server is telling the client what to change; retrying the identical request without a fix will reproduce the same error. In practice you will meet 401 when an expired, missing or malformed authorization bearer token; http basic auth with wrong or absent credentials; a session cookie that expired or was never sent cross-origin. On the client side, refresh or obtain credentials and retry with a proper Authorization header; check token expiry, audience and scope claims before assuming the server is wrong. On the server side, always send WWW-Authenticate with the scheme (Bearer, Basic) and realm; distinguish 401 (who are you?) from 403 (I know you, but no).
When 401 happens
- An expired, missing or malformed Authorization bearer token
- HTTP Basic auth with wrong or absent credentials
- A session cookie that expired or was never sent cross-origin
How to fix it
On the client
Refresh or obtain credentials and retry with a proper Authorization header; check token expiry, audience and scope claims before assuming the server is wrong.
On the server
Always send WWW-Authenticate with the scheme (Bearer, Basic) and realm; distinguish 401 (who are you?) from 403 (I know you, but no).
Should you retry?
Not retryable as-is: change the request (credentials, payload, URL) first.
Debugging steps
When 401 appears unexpectedly, start by capturing the full exchange: 'curl -i' or the browser DevTools Network tab shows the exact status line and response headers, which often carry the real explanation. Read the error body first — well-designed APIs name the offending field or missing credential. Then diff your actual request, headers included, against the documented contract. For this code specifically, check whether the most common trigger applies to your situation: an expired, missing or malformed authorization bearer token. Compare your exchange against the raw HTTP example above. If you operate the server, correlate the timestamp with application and proxy logs using the request id; if you are the client, verify the request shape against the API documentation before changing anything or retrying.
Notes & gotchas
The name is historical and misleading: 401 is about authentication (identity), not authorization (permission). A missing WWW-Authenticate header makes the response non-compliant and leaves clients guessing how to authenticate.
Examples
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="api", error="invalid_token"
Content-Type: application/json
Content-Length: 105
{
"error": "unauthorized",
"message": "An expired, missing or malformed Authorization bearer token"
} curl -i -H "Authorization: Bearer $TOKEN" https://api.example.com/v1/resource const res = await fetch("https://api.example.com/v1/resource");
if (res.status === 401) {
const token = await refreshAccessToken(); // then retry ONCE with the new token
return fetch(url, { headers: { Authorization: `Bearer ${token}` } });
} import requests
res = requests.get("https://api.example.com/v1/resource", timeout=10)
if res.status_code == 401:
token = refresh_access_token()
res = requests.get(url, headers={"Authorization": f"Bearer {token}"}, timeout=10) Frequently asked questions
401 vs 403 in one sentence?
401 means authentication is missing or invalid and might succeed with credentials; 403 means the server understood the credentials but refuses access regardless.
Why does my valid token still get 401?
Common culprits: clock skew making exp/nbf fail, wrong audience, token signed by a different key after rotation, or a proxy stripping the Authorization header.
Are 4xx errors always the client's fault?
By contract yes — the server asserts the request must change. In practice servers sometimes misuse 4xx for their own problems (e.g. 400 for internal validation bugs), so read the error body before blaming your client code.
Should I retry 4xx responses automatically?
Generally no: the server says the request must change. Exceptions are 408 (fresh connection), 425 (after handshake) and 429 (after Retry-After). Blind retries of 400/401/403/404 just burn quota.
Try the JSON Validator
Check JSON syntax and locate errors by line and column.