Accepted
Production Risk
Low. It correctly communicates that a task is queued. The application must have a separate mechanism (like polling or webhooks) for the client to check the final outcome.
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. There is no facility for re-sending a status code from an asynchronous operation.
- 1A request is submitted to a queue for batch processing.
- 2An API call triggers a long-running background job, like video encoding.
- 3The server accepts a request that will be handled by another process or server.
A user submits a request to generate a complex, multi-page PDF report which will be processed in the background.
POST /api/reports HTTP/1.1
Host: example.com
Content-Type: application/json
{ "report_type": "annual_summary" }expected output
HTTP/1.1 202 Accepted
Fix
Return 202 for queued async jobs and provide a status polling URL
// Express — correct use of 202 Accepted for async job submission
app.post('/api/reports', async (req, res) => {
const job = await jobQueue.enqueue('generate-report', req.body);
res.status(202).json({
message: 'Report generation accepted and queued.',
jobId: job.id,
statusUrl: `/api/jobs/${job.id}`,
});
});
// Polling endpoint the client uses to check job progress
app.get('/api/jobs/:id', async (req, res) => {
const job = await jobQueue.find(req.params.id);
res.json({ status: job.status, result: job.result ?? null });
});
// Client-side: poll until complete
async function pollUntilDone(statusUrl) {
while (true) {
const r = await fetch(statusUrl);
const { status, result } = await r.json();
if (status === 'done') return result;
await new Promise(resolve => setTimeout(resolve, 2000));
}
}Why this works
202 Accepted is correct when the server has accepted the request for processing but cannot complete it synchronously. Always include a way for the client to check the outcome — typically a statusUrl for polling or instructions for a webhook. Without this, the client has no way to know if the operation eventually succeeded or failed.
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev