Authentication required: understand and resolve.
The HTTP 401 "Unauthorized" error is the standard response code indicating that authentication is required to access a resource. Despite its name, this code concerns authentication (proving identity) and not authorization (having permissions).
The 401 code is accompanied by a WWW-Authenticate header that specifies the accepted authentication mechanism (Basic, Bearer, Digest, etc.). The client can then retry with the correct credentials. This is the behavior that triggers browser login popups for sites protected by Basic Auth.
For monitoring protected APIs, 401 is a critical signal. An unexpected 401 may indicate token expiration, access revocation, or a change in authentication configuration. MoniTao allows monitoring these endpoints by configuring the appropriate authentication.
The 401 error occurs when authentication fails. Here are the most common causes:
These two codes are often confused but have distinct meanings:
Based on the identified cause, here are the solutions to apply:
Here's how to configure authentication for different types of requests:
# cURL - Basic Auth
curl -u username:password https://api.example.com/resource
# cURL - Bearer Token
curl -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIs..." https://api.example.com/resource
# JavaScript fetch - Bearer Token
fetch("https://api.example.com/resource", {
headers: {
"Authorization": "Bearer " + accessToken
}
})
# PHP - Server-side verification
$authHeader = $_SERVER["HTTP_AUTHORIZATION"] ?? "";
if (!preg_match("/Bearer\s+(\S+)/", $authHeader, $matches)) {
http_response_code(401);
header("WWW-Authenticate: Bearer");
exit(json_encode(["error" => "Token required"]));
}
The WWW-Authenticate header in the 401 response tells the client which authentication mechanism to use.
Monitoring protected APIs requires specific configuration:
It's a historical misnomer in the HTTP specification. The correct term would be "Unauthenticated". RFC 7235 maintains this name for backward compatibility.
Configure a MoniTao monitor with an Authorization: Bearer
Check: 1) Exact header format (Bearer with capital B, single space). 2) No invisible spaces around the token. 3) Token not revoked server-side.
Use long-lived tokens for monitoring, or implement automatic rotation with alerts N days before expiration.
This is a server-side implementation error. 401 should only be used when identity is not established. For an authenticated user without permissions, it's 403.
Implement a refresh token system. When the access token expires (401), use the refresh token to get a new one without re-requesting credentials.
The HTTP 401 Unauthorized error signals an authentication problem: missing, expired, or invalid credentials. Understanding the difference with 403 (authorization) enables implementing more robust security systems.
MoniTao supports monitoring protected APIs with Basic Auth and Bearer Token. Configure your monitors with dedicated long-lived tokens and receive alerts for unexpected 401s, signaling an authentication problem to resolve immediately.
Start free, no credit card required.