/ HTTP Status Codes

407 Proxy Authentication Required

The 407 Proxy Authentication Required HTTP status code is a response from a proxy server indicating that the client must authenticate itself to gain access to the requested resource. This status code is similar to 401 Unauthorized but specifically applies to clients that need to authenticate with a proxy server.

When is the 407 Status Code Used?

A proxy server may require clients to authenticate themselves before they can access the requested resource. This can be for various reasons, such as security, access control, or tracking usage. When a client sends a request to a proxy server without providing the required authentication credentials, the proxy server responds with a 407 Proxy Authentication Required status code.

How Does the 407 Status Code Work?

When a client sends a request to a proxy server that requires authentication, the proxy server responds with a 407 Proxy Authentication Required status code and includes a Proxy-Authenticate header. This header specifies the type of authentication required and any additional information needed to complete the authentication process.

Example Request and Response

Here’s an example of a client making an HTTP request to a proxy server without providing the required authentication credentials:

GET http://example.com/resource HTTP/1.1
Host: example.com

The proxy server responds with a 407 Proxy Authentication Required status code and includes the Proxy-Authenticate header:

HTTP/1.1 407 Proxy Authentication Required
Proxy-Authenticate: Basic realm="Proxy Server"

In this example, the proxy server requires the client to authenticate using the Basic authentication scheme.

Handling the 407 Status Code in a Client Application

When a client application receives a 407 Proxy Authentication Required response, it should follow these steps:

  1. Parse the Proxy-Authenticate header to determine the required authentication scheme and any additional information needed for authentication.
  2. Obtain the necessary authentication credentials, typically a username and password, either from the user or from a stored configuration.
  3. Encode the credentials according to the specified authentication scheme. For example, in Basic authentication, the credentials are base64-encoded.
  4. Resend the original request with the Proxy-Authorization header containing the encoded credentials.

Example of Handling the 407 Status Code

Here’s an example of a client handling the 407 Proxy Authentication Required status code and resending the request with the Proxy-Authorization header:

import requests
from requests.auth import HTTPProxyAuth

proxy_url = "http://proxy.example.com:8080"
url = "http://example.com/resource"
auth = HTTPProxyAuth("username", "password")

response = requests.get(url, proxies={"http": proxy_url, "https": proxy_url}, auth=auth)

if response.status_code == 407:
    print("Proxy authentication failed")
else:
    print(response.text)

In this example, we use the requests library in Python to handle the proxy authentication. The HTTPProxyAuth class takes care of encoding the credentials and adding the Proxy-Authorization header to the request.

Summary

The 407 Proxy Authentication Required HTTP status code indicates that a client must authenticate itself with a proxy server to access the requested resource. When receiving this status code, a client application should parse the Proxy-Authenticate header, obtain the required authentication credentials, and resend the request with the Proxy-Authorization header containing the encoded credentials.

Was this helpful?

Thanks for your feedback!