HTTP status code 401 indicates that the requested resource requires authentication. If the client fails to provide valid authentication credentials or if the provided credentials cannot be verified by the server, the server will return this status code.
The 401 status code is typically associated with the following situations:
- Missing Authentication Information: The client has not provided any authentication information.
- Authentication Failure: The provided authentication information cannot be verified by the server.
- Authentication Required: The server requires the client to provide authentication information to access the resource.
When the server returns a 401 status code, it typically includes a WWW-Authenticate
field in the response header, indicating how to authenticate. This field may contain an authentication scheme (such as Basic or Digest) along with any parameters needed for authentication.
For example, the server might send the following response:
HTTP/1.1 401 Unauthorized
Content-Type: text/html
Content-Length: 281
WWW-Authenticate: Basic realm="Access to the staging site"
<html>
<head><title>Authentication Required</title></head>
<body>
<h1>Authentication Required</h1>
<p>You need to provide a valid username and password to access this resource.</p>
</body>
</html>
In this example, the status code 401 indicates that access is denied due to the lack of valid authentication information. The WWW-Authenticate
header indicates that the client needs to use the Basic authentication scheme and provides a protected realm named “Access to the staging site.”
When the client receives a 401 response, it should prompt the user to enter the correct authentication information (such as a username and password) and then resend the request using the appropriate authentication scheme.
It is important to note that, for security reasons, when using the Basic authentication scheme, the username and password should be sent over HTTPS (rather than HTTP) to prevent sensitive information from being intercepted.
Related 4xx error code
- 400 Bad Request
- 402 Payment Required
- 403 Forbidden
- 404 Not Found
- 405 Method Not Allowed
- 406 Not Acceptable
- 407 Proxy Authentication Required
- 408 Request Timeout
- 409 Conflict
- 410 Gone
- 411 Length Required
- 412 Precondition Failed
- 413 Payload Too Large
- 414 URI Too Long
- 415 Unsupported Media Type
- 416 Range Not Satisfiable
- 417 Expectation Failed
- 418 I’m a teapot
- 421 Misdirected Request
- 422 Unprocessable Entity (WebDAV)
- 423 Locked (WebDAV)
- 424 Failed Dependency (WebDAV)
- 425 Too Early
- 426 Upgrade Required
- 428 Precondition Required
- 429 Too Many Requests
- 431 Request Header Fields Too Large
- 451 Unavailable For Legal Reasons
- 499 Client Closed Request