Difference between http response status code 402 and 403
Categories:
HTTP Status Codes 402 vs. 403: Understanding Payment Required and Forbidden
Explore the key differences between HTTP 402 Payment Required and 403 Forbidden status codes, their use cases, and how to implement them correctly in web applications.
HTTP status codes are essential for understanding the outcome of a request to a server. Among the 4xx client error codes, 402 Payment Required
and 403 Forbidden
are often confused due to their similar implication of access denial. However, their underlying reasons and intended use cases are distinct. This article will clarify the differences, providing guidance on when to use each code appropriately.
Understanding 402 Payment Required
The 402 Payment Required
status code is reserved for future use, indicating that the client needs to make a payment to access the requested resource. It was originally created for a digital cash or micro-payment system, but that system was never fully developed or widely adopted. Consequently, it's rarely used in standard web applications today. However, some APIs and applications have repurposed it to indicate that a request cannot be fulfilled until the client completes a payment or subscription.
When 402
is returned, the server is explicitly stating that the requested action requires a payment. The response body should typically include details on how to make the payment, the required amount, or a link to a payment portal. It implies that with a successful payment, the client would then be granted access.
HTTP/1.1 402 Payment Required
Content-Type: application/json
{
"error": "Payment Required",
"message": "Please subscribe to access this content.",
"payment_url": "https://example.com/subscribe"
}
A typical HTTP 402 response indicating a payment is needed.
Server-side logic for HTTP 402
Understanding 403 Forbidden
The 403 Forbidden
status code signifies that the server understood the request but refuses to authorize it. Unlike 401 Unauthorized
(which means authentication is required), 403
implies that the client is authenticated (or authentication is irrelevant), but still lacks the necessary permissions to access the resource. The server knows who the client is, but for some reason (e.g., insufficient user roles, IP restrictions, or specific access policies), it denies access.
Crucially, 403
does not suggest that payment is required. It's about authorization. Even if the client were to pay, it might still be denied access if the underlying authorization policies haven't changed. The server will not fulfill the request and should not repeat it without changes to the client's authorization status or the request itself.
HTTP/1.1 403 Forbidden
Content-Type: application/json
{
"error": "Forbidden",
"message": "You do not have permission to access this resource."
}
A typical HTTP 403 response indicating insufficient permissions.
Server-side logic for HTTP 403
Key Differences and When to Use Which
The fundamental distinction lies in the reason for denial:
- 402 Payment Required: Access is denied because a payment or subscription is outstanding. The client could gain access by fulfilling a financial obligation.
- 403 Forbidden: Access is denied due to insufficient authorization or permissions, regardless of payment. The client cannot gain access even if they attempt to pay, unless their authorization status changes.
Consider these scenarios:
- Subscription Service: A user tries to watch a premium video. If their subscription has expired, return
402 Payment Required
with a link to renew. If they are logged in but their account type (e.g., 'free tier') does not permit access to premium content, return403 Forbidden
. - API Access: An API endpoint requires a paid plan. If the API key belongs to an unpaid account, return
402
. If the API key belongs to a paid account but the specific endpoint requires an 'admin' role and the key is for a 'user' role, return403
. - File System Access: A user tries to access a file on a server. If the file is part of a paid content package they haven't bought,
402
. If the file is restricted to certain user groups and the user isn't in that group (even if they have a paid account),403
.
402
for payment-related denials and 403
for authorization issues helps clients understand how to resolve the error and improves API usability.