Products

Solutions

Resources

Search for API Security Tests

/

/

401 Status Code - Unauthorized

401 Status Code - Unauthorized

In this section you will learn about 401 Status Code, what is it, its components and examples.

In this section you will learn about 401 Status Code, what is it, its components and examples.

401 Status Code - Unauthorized
401 Status Code - Unauthorized
401 Status Code - Unauthorized

Luke Stephens

Luke Stephens

Luke Stephens

What is HTTP Status Code 401 - Unauthorized?

The HTTP Status Code 401, labeled as "Unauthorized", is a client error response code. It signifies that the client must authenticate itself to get the requested response. The 401 status code points out that the request has not been applied because it lacks valid authentication credentials for the target resource.

Understanding of 401 Unauthorized

  • Authentication Required: The 401 Unauthorized status code indicates that the client hasn't been authorized to access the requested resource. This typically means the client has not provided valid credentials or hasn't provided any credentials at all.

HTTP/1.1 401 Unauthorized
  • WWW-Authenticate Header: This header often accompanies the 401 Unauthorized status code. It defines the authentication method that should be employed to access the resource.

WWW-Authenticate: Basic realm="Example"

Why 401 Unauthorized?

The 401 Unauthorized status code serves as a gatekeeper mechanism. When a resource or endpoint is protected and requires authentication, the server will use this status code to challenge the client to provide valid credentials.

Characteristics of 401 Unauthorized

  • Initial Entry Point for Authentication: Before gaining access to a resource, the client might initially receive a 401 Unauthorized response, prompting it to provide credentials.

  • Clear Distinction from 403 Forbidden: While both 401 Unauthorized and 403 Forbidden pertain to permissions, the 401 code specifically indicates that authentication is required and has either not been provided or is invalid.

How does 401 Unauthorized Work?

  1. Client Sends a Request:

    The client sends a request to a protected server resource without authentication credentials.

GET /secure-data HTTP/1.1
Host: www.example.com
  1. Server Responds with 401:

    The server detects the lack of credentials and sends a 401 Unauthorized response, often accompanied by a WWW-Authenticate header indicating the required authentication method.

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="Secure Data"

Example of 401 Unauthorized

Accessing a Secure Endpoint:

GET /user-profile HTTP/1.1
Host: www.example.com

Response:

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="User Profile", error="invalid_token", error_description="The access token expired"

In this scenario, the client attempts to access a user profile without an access token or with an expired token. The server responds with a 401 Unauthorized, providing details about the authentication error.

Conclusion

The 401 Unauthorized status code plays a fundamental role in the realm of web security. It acts as a sentry, ensuring that only clients with valid credentials can access protected resources. By understanding and effectively managing the 401 Unauthorized status code, developers and server administrators can maintain secure endpoints while offering clear feedback to clients, paving the way for secure and efficient web interactions.

On this page

Protect your APIs from attacks now

Protect your APIs from attacks now

Protect your APIs from attacks now

Explore more from Akto

Blog

Be updated about everything related to API Security, new API vulnerabilities, industry news and product updates.

Events

Browse and register for upcoming sessions or catch up on what you missed with exclusive recordings

CVE Database

Find out everything about latest API CVE in popular products

Test Library

Discover and find tests from Akto's 100+ API Security test library. Choose your template or add a new template to start your API Security testing.

Documentation

Check out Akto's product documentation for all information related to features and how to use them.