Lab-tested: Python 3.13 mock API + curl · 2 Dec 2026 · Every 401 below was reproduced and fixed in our lab
Fix Box — Try This First (10 Seconds)
A 401 means the server refused your credentials: they are missing, expired, or malformed. Read the WWW-Authenticate response header first — it names the scheme the server expects.
Then check your Authorization header shape: scheme, one space, then the token. Still 401? The token is probably expired, so refresh it instead of retrying the old one.
What Does 401 Unauthorized Mean?
401 Unauthorized means the request lacks valid authentication credentials for the target resource. The server must send a WWW-Authenticate header that tells the client how to authenticate.
Despite the name, 401 is about authentication (who you are), not authorization (what you may do) — that second case is 403.
The Exact Messages You Will See
Servers describe the failure in the body and in the challenge header. The three shapes we reproduced most often in the lab are an empty credential, an expired token, and a malformed header.
Each one produces a different WWW-Authenticate value, which is why reading that header comes before changing any code.
Start Here: The 401 Decision Tree
First, does the response carry a WWW-Authenticate header at all? If it does not, a proxy or WAF in front of the app probably answered, not the app itself.
If it does, compare its scheme (Bearer, Basic, ApiKey) with the scheme you actually sent. A mismatch there explains most 401s we see. The 401 Checker reads the challenge for you.
Our Lab: Six Requests, Three 401s, Two Fixes
We ran a local mock API that protects /api/profile with a Bearer token and /api/basic with Basic Auth. Every request below was sent with curl on 2 December 2026, and the server log matched each result.
Case 1 — no Authorization header:
HTTP/1.0 401 Unauthorized
WWW-Authenticate: Bearer realm="api"
Content-Type: application/json
{"error": "unauthorized", "error_description": "Authentication credentials were not provided."}
Case 2 — expired Bearer token:
HTTP/1.0 401 Unauthorized
WWW-Authenticate: Bearer realm="api", error="invalid_token", error_description="The access token expired"
Content-Type: application/json
{"error": "invalid_token", "error_description": "The access token expired"}
Case 3 — malformed header (token without the Bearer prefix): the server answered the same 401 as Case 1. A header it cannot parse is treated as no credential at all.
This is the silent failure that wastes the most debugging time, because the token itself was valid.
Case 4 — the fix, valid Bearer token:
HTTP/1.0 200 OK
Content-Type: application/json
{"id": 1, "name": "Lab User", "plan": "pro"}
Cases 5–6 — Basic Auth: without credentials the server answered 401 with WWW-Authenticate: Basic realm="Restricted". With correct credentials (curl -u user:pass) it answered 200 with {"ok":true,"via":"basic"}.
The server log recorded all six requests in order: 401, 401, 401, 200, 401, 200.
Fix It by Auth Type
For JWT/Bearer, check expiry (exp) and audience first, then the prefix. For API keys, confirm the header name the service expects — X-Api-Key and Authorization are not interchangeable.
For Basic Auth, verify the Base64 encoding and that no proxy strips the header. Each scheme has a dedicated guide on this site with its own lab run.
Fix It by Platform
On WordPress, REST API 401s usually come from cookie authentication without a nonce, or a security plugin blocking Application Passwords. On Nginx, auth_request subrequests and stripped Authorization headers behind FastCGI are the classic causes.
On Apache, .htaccess AuthType rules and CGI header handling produce the same symptom.
What Did Not Work
Retrying the expired token never worked — the server kept answering invalid_token, exactly as it should. Sending the valid token without the Bearer prefix also failed, and the error message did not say why.
Both failures taught the same lesson: fix the credential shape and freshness, not the retry count.
FAQ
Is a 401 my fault or the server's? Almost always the client's: the credential is missing, expired, or malformed. A server-side 401 bug usually shows up as a missing WWW-Authenticate header.
What is the difference between 401 and 403? 401 means "authenticate first"; 403 means "authenticated, but not allowed". Our full comparison lives in the 401 vs 403 guide.
Why did I get a 401 on my first request? You probably sent no credential, or the wrong scheme. Read WWW-Authenticate and match it exactly.
Should I keep retrying a 401? No. A 401 will not heal by itself; refresh the token or fix the header, then retry once.
Can a proxy cause a 401? Yes. Some proxies strip the Authorization header before it reaches the app, so the app honestly reports no credential.
Sources and date
Lab run: 2 December 2026, Python 3.13.5 mock API on localhost, curl client. All tokens in this article are lab values, never real credentials.






Leave a Reply