202 Accepted
Description
The 202 Accepted
status code indicates that the request has been accepted for processing, but the processing has not been completed. The request might or might not eventually be acted upon, as it might be disallowed when processing actually takes place.
This status code is particularly useful for asynchronous operations, where the server needs time to process the request and cannot immediately provide a response. It essentially tells the client "we've received your request and will process it later."
The 202 Accepted
status is commonly used in scenarios involving background processing, batch operations, or any situation where request processing is expected to take a significant amount of time.
Syntax
The server responds with a 202 Accepted
status and typically includes information about the request's status:
HTTP/1.1 202 Accepted
Content-Type: [appropriate media type]
Content-Length: [length in bytes]
[Response body containing information about the request status and possibly how to check on its progress]
Examples
Asynchronous Job Processing Example
A client submits a request to generate a complex report:
Client Request:
POST /reports/generate HTTP/1.1
Host: api.example.com
Content-Type: application/json
Content-Length: 187
{
"report_type": "annual_financial",
"year": 2023,
"departments": ["sales", "marketing", "engineering"],
"include_charts": true,
"format": "pdf"
}
Server Response:
HTTP/1.1 202 Accepted
Content-Type: application/json
Content-Length: 165
{
"status": "processing",
"job_id": "job-12345",
"estimated_completion_time": "2023-06-15T17:30:00Z",
"status_url": "/jobs/job-12345"
}
Email Sending Example
A client requests to send a bulk email campaign:
Client Request:
POST /email/campaigns HTTP/1.1
Host: marketing.example.com
Content-Type: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Content-Length: 358
{
"campaign_name": "Summer Sale 2023",
"subject": "Don't Miss Our Summer Sale - 50% Off Everything!",
"content": "...",
"recipient_list_id": "list-789",
"scheduled_time": "2023-06-16T09:00:00Z"
}
Server Response:
HTTP/1.1 202 Accepted
Content-Type: application/json
Content-Length: 198
{
"campaign_id": "camp-456",
"status": "queued",
"recipient_count": 25000,
"estimated_start_time": "2023-06-16T09:00:00Z",
"status_url": "/campaigns/camp-456/status"
}
Data Processing Example
A client submits a large dataset for processing:
Client Request:
POST /data/process HTTP/1.1
Host: analytics.example.com
Content-Type: application/json
Content-Length: 1048576
{
"dataset_name": "user_behavior_q2",
"analysis_type": "clustering",
"parameters": {
"algorithm": "k-means",
"clusters": 5,
"iterations": 1000
},
"data": [
/* Large array of data points */
]
}
Server Response:
HTTP/1.1 202 Accepted
Content-Type: application/json
Content-Length: 187
{
"job_id": "analysis-789",
"status": "queued",
"position_in_queue": 3,
"estimated_start_time": "2023-06-15T18:45:00Z",
"status_url": "/jobs/analysis-789"
}
Summary
The 202 Accepted
status code is a powerful tool for handling asynchronous operations in web applications and APIs. It allows servers to acknowledge receipt of a request without having to immediately process it, which is particularly valuable for resource-intensive or time-consuming operations. By providing a mechanism to check on the status of the request (typically through a status URL), it enables clients to monitor the progress of their request while the server processes it in the background.