/ HTTP Status Codes

202 Accepted

The 202 Accepted status code is an HTTP response status code that indicates a request has been accepted for processing, but the processing has not yet been completed. This status code is typically used when a server is performing an asynchronous operation, as the server cannot guarantee a successful outcome or provide an immediate response.

Purpose of the 202 Accepted Status Code

The primary purpose of the 202 Accepted status code is to inform the client that the server has received and accepted their request, but the processing is still ongoing. This status code is particularly useful in situations where a request might take a significant amount of time to process or when the server is processing the request asynchronously.

The 202 Accepted status code also implies that the server is not obligated to send a final response, as the request might be processed by another system or at a later time. However, it is good practice to provide a way for the client to track the progress of the request, such as through a Location header or a status endpoint.

When to Use the 202 Accepted Status Code

You should use the 202 Accepted status code in situations where:

  1. The request has been accepted for processing, but the processing has not yet been completed.
  2. The server cannot guarantee a successful outcome for the request.
  3. The server is processing the request asynchronously and cannot provide an immediate response.

Some common use cases for the 202 Accepted status code include:

  • Long-running tasks, such as data processing or file conversions, where the server cannot provide an immediate response.
  • Asynchronous processing, where the server delegates the request to another system or processes it at a later time.
  • Actions that require manual intervention or approval, such as a human review process.

Example Request and Response

Now, let’s look at an example of a request and response that may result in a 202 Accepted status code.

Request

POST /api/v1/orders HTTP/1.1
Host: example.com
Content-Type: application/json
Authorization: Bearer your_token_here

{
  "product_id": 42,
  "quantity": 3
}

In this example, the client is making a POST request to an API to create a new order. The server may need to perform several tasks before the order is processed, such as checking inventory, reserving the items, and processing the payment.

Response

HTTP/1.1 202 Accepted
Content-Type: application/json
Location: /api/v1/orders/123/status

{
  "message": "Your order has been accepted and is being processed.",
  "order_id": 123,
  "status": "processing"
}

In this example, the server responds with a 202 Accepted status code, indicating that the request has been accepted for processing, but the processing has not yet been completed. The server also includes a Location header, which points to a status endpoint that the client can use to track the progress of the request.

Summary

The 202 Accepted status code is an important tool for communicating the state of a request that has been accepted for processing but has not yet been completed. By understanding the purpose, use cases, and examples of the 202 Accepted status code, you can better design and implement APIs that handle asynchronous or long-running tasks. Remember to provide a way for clients to track the progress of their requests, such as through a Location header or a status endpoint, to ensure a positive user experience.

Was this helpful?

Thanks for your feedback!