Lab-tested: Python 3.13.5 mock API (lab) · 2 Dec 2026 · Reproduced and fixed in our lab
Fix Box — Try This First (10 Seconds)
A Basic Auth 401 means the server got no credentials, or the wrong ones. First, confirm your client actually sends Authorization: Basic <base64> — many libraries only send it after a challenge.
Second, verify the username and password pair outside your code, with curl. Third, read the WWW-Authenticate header: the realm and charset tell you what the server expects.
Still stuck? Inspect the endpoint with the 401 Checker or start from the pillar 401 Unauthorized: Causes and How to Fix It.
Table of Contents
- How Basic Auth Is Supposed to Work
- The Exact Messages (Verbatim from Our Lab)
- Cause 1: No Credentials Sent
- Cause 2: Wrong Username or Password
- Cause 3: Encoding and Charset Problems
- Cause 4: The Header Never Reaches the App
- Our Lab Reproduction
- The Fix, Step by Step
- What Did Not Work
- Prevention
- FAQ
- Sources and Date
- References
How Basic Auth Is Supposed to Work
Basic Auth is the oldest HTTP authentication scheme, defined in RFC 9110 and RFC 7617. The client sends a username and password joined by a colon, Base64-encoded, in the Authorization header.
Base64 is encoding, not encryption. Anyone who sees the header can decode it. That is why Basic Auth belongs only on HTTPS connections.
The dance has two steps. The client requests a protected resource, often without credentials.
The server answers 401 with a challenge like WWW-Authenticate: Basic realm="lab", charset="UTF-8". The client retries with the header attached.
If your client never retries, or retries with wrong values, every request ends at 401. Most failures are one of those two.
The Exact Messages (Verbatim from Our Lab)
We reproduced the Basic failures against a local mock API on 2 Dec 2026. The lab endpoint requires one username/password pair. Captures below are verbatim, with the lab password never shown:
HTTP/1.0 401 Unauthorized
WWW-Authenticate: Basic realm="lab", charset="UTF-8"
{"error": "missing_credentials"}
HTTP/1.0 401 Unauthorized
WWW-Authenticate: Basic realm="lab", charset="UTF-8"
{"error": "invalid_credentials"}
# AFTER (correct credentials)
HTTP/1.0 200 OK
{"ok": true, "user": "labuser"}
Notice the challenge is identical in both failures. The server deliberately does not tell you whether the username or the password was wrong.
That silence is a security feature. It also means you must test the pair as a unit.
Cause 1: No Credentials Sent
The most common Basic 401 is an empty one. Browsers prompt for credentials after a challenge, but scripts and API clients often do not.
They receive the 401, log it, and stop. Our lab case 9 sent no header and got missing_credentials with the Basic challenge.
Check your client configuration first. Many libraries need credentials set explicitly, or a flag to send them preemptively.
Also check redirects: clients commonly drop Authorization on cross-host redirects. A 401 that appears only after a redirect is usually this.
Cause 2: Wrong Username or Password
The header arrived, the pair did not match. Our lab sent the right username with a wrong password and got invalid_credentials.
Real causes: a rotated password not updated in one config, the wrong environment's credentials, or a username that includes a domain prefix the server does not expect (or the reverse).
Test the pair with curl before touching code:
curl -si -u username https://your-host.example/protected
curl will prompt for the password without echoing it. If curl also gets 401, the credentials are wrong, full stop. If curl succeeds, your application is sending something different.
Cause 3: Encoding and Charset Problems
The pair must be username:password, Base64-encoded. Passwords containing a colon are legal — only the first colon separates.
Usernames cannot contain a colon at all. Non-ASCII characters are the classic trap: RFC 7617 added the charset="UTF-8" parameter because older clients encoded with ISO-8859-1. A password with accented characters can encode differently in your client and on the server.
Our lab challenge advertises charset="UTF-8". If your password has non-ASCII characters and logins fail, test with an ASCII-only password to isolate encoding.
Also watch for trailing newlines when credentials come from files or environment variables. A newline inside the Base64 input changes everything.
Cause 4: The Header Never Reaches the App
Sometimes the client sends the header and the server stack eats it. The classic case is Apache with CGI or FastCGI, where Authorization is not passed to the application by default.
PHP applications behind such setups see no credentials and answer 401 forever. Proxies can strip the header too, and some do it on purpose for security.
Prove it with a tiny echo endpoint or server log that records header presence (not values). If the header is present at the edge but absent in the app, fix the server configuration to forward it.
On Apache, the usual fix involves CGIPassAuth or a rewrite rule that copies the header into an environment variable.
Our Lab Reproduction
Stack: Python 3.13.5 mock API on localhost, tested 2 Dec 2026. Endpoint /api/basic challenges with Basic realm="lab", charset="UTF-8".
We captured three states: no credentials, wrong password, correct pair. Before: both failures returned 401 with the identical challenge. After: the correct pair returned 200 OK and the username.
Honest lab note: a mock proves the client-side flow and the challenge format. Real servers (Apache, Nginx, IIS) add their own layers — realm names, .htpasswd files, and header-forwarding quirks.
The diagnostic order here — presence, pair, encoding, forwarding — is what transfers. Full captures are stored with this package.
The Fix, Step by Step
- Confirm the challenge and test the pair with curl:
curl -si https://your-host.example/protected
curl -si -u username https://your-host.example/protected
- If curl fails, fix the credentials themselves: correct environment, current password, exact username format.
- If curl works, make your client send the header explicitly and preemptively:
import base64, urllib.request, urllib.error
pair = base64.b64encode(b"username:password").decode()
req = urllib.request.Request(
"https://your-host.example/protected",
headers={"Authorization": "Basic " + pair},
)
try:
with urllib.request.urlopen(req) as r:
print(r.status, r.read().decode())
except urllib.error.HTTPError as e:
print(e.code, e.headers.get("WWW-Authenticate"))
- If the app still sees nothing, check header forwarding in your server stack (Apache CGI/FastCGI is the usual suspect).
- Verify 200 over HTTPS only. Never debug Basic Auth over plain HTTP with real passwords.
What Did Not Work
Guessing which half of the pair was wrong did not work, because the server refuses to say. Our lab returned the identical challenge for missing and wrong credentials. Treat the pair as one unit and test it as one unit.
Retrying without changing anything also failed, obviously but commonly. A client that does not attach credentials after the challenge will loop on 401 forever. If your logs show repeated 401s with no Authorization header recorded, that is your bug.
Encoding the password alone, without the username and colon, produces a well-formed header that always fails. The format is the pair, always: username:password.
Prevention
Use HTTPS everywhere Basic Auth appears; Base64 is not protection. Prefer token-based schemes for APIs, and keep Basic Auth for simple internal tools.
Rotate the password on personnel changes, and store it in a secret manager, not in scripts. For the wider map, see the pillar 401 Unauthorized: Causes and How to Fix It, the 401 Checker, 401 JWT/Bearer Token Errors, and 401 Invalid API Key Errors.
FAQ
Why does the browser keep asking for my password? The server rejected the pair you entered and challenged again.
Browsers re-prompt on each 401. Verify the exact username format and current password with curl.
Is Basic Auth safe to use? Only over HTTPS, and only for low-risk uses.
The credentials are merely Base64-encoded and sent on every request. For public APIs, use token-based auth instead.
My password has special characters. Is that a problem? It can be.
Colons are fine in passwords, not in usernames. Non-ASCII characters depend on charset handling; test with an ASCII password to isolate it.
Why does curl work but my app gets 401? Your app sends different credentials, no header, or the server stack drops the header before the app. Compare header presence in server logs, and check Apache CGI/FastCGI forwarding.
What does the realm value mean? It names the protection space the credentials belong to.
Browsers use it to decide which saved password to offer. It does not change how you encode the pair.
Sources and Date
Lab tests run 2 Dec 2026 on Python 3.13.5 (mock API, localhost). Captures are from that run; the lab password is never printed. References verified live on 2 Dec 2026.
References
- RFC 9110, HTTP Semantics, Section 15.5.2 (401 Unauthorized): https://www.rfc-editor.org/rfc/rfc9110.html
- RFC 7617, The 'Basic' HTTP Authentication Scheme: https://www.rfc-editor.org/rfc/rfc7617.html
- MDN Web Docs, 401 Unauthorized: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/401
- MDN Web Docs, WWW-Authenticate: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/WWW-Authenticate
- MDN Web Docs, Authorization: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization






Leave a Reply