The 401 Unauthorized HTTP status code indicates that the client request has not been completed because it lacks valid authentication credentials. The server requires authentication, and the client either did not provide credentials or provided invalid ones.
WWW-Authenticate headerA 401 response is returned when authentication is required and has failed or has not yet been provided. This typically happens when accessing protected resources such as dashboards, APIs, or admin panels.
// Server responds with 401 when authentication fails
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="Access to admin"
The browser or client receives a 401 status and may prompt the user to log in again. In APIs, the client usually needs to resend the request with valid tokens or credentials.
See how the server responds based on your credentials below:
// JavaScript fetch example handling 401 Unauthorized
fetch("/api/profile")
.then(response => {
if (response.status === 401) {
throw new Error("Unauthorized access");
}
return response.json();
})
.catch(error => console.error(error.message));
Simulator: Enter the correct token to access the data.
Hint: The correct token is secret123
WWW-Authenticate headers