0
0
Rest APIprogramming~10 mins

Long-running operations (async responses) in Rest API - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Long-running operations (async responses)
Client sends request
Server receives request
Server starts long task
Server immediately responds with 202 Accepted
Client polls or waits for status URL
Server processes task in background
Client receives final result when ready
The client sends a request for a long task. The server starts it and immediately replies with a 202 status and a status URL. The client checks this URL until the task finishes and gets the final result.
Execution Sample
Rest API
POST /start-task HTTP/1.1
Response: 202 Accepted
Location: /task-status/123

GET /task-status/123
Response: 200 OK
{ "status": "completed", "result": "done" }
Client starts a long task, gets a 202 with a status URL, then polls that URL until the task completes.
Execution Table
StepActionRequest/ResponseStatus CodeNotes
1Client sends start requestPOST /start-taskN/ARequest to start long task
2Server accepts request202 Accepted202Task started asynchronously, returns Location header
3Client polls status URLGET /task-status/123N/AClient checks if task is done
4Server responds with status{ "status": "processing" }200Task still running
5Client polls againGET /task-status/123N/AClient waits for completion
6Server responds with result{ "status": "completed", "result": "done" }200Task finished, result returned
7Client uses resultN/AN/AClient processes final data
ExitTask complete, no more pollingN/AN/APolling stops as task is done
💡 Polling stops when server returns status 'completed' with the final result.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
task_statusnoneprocessingprocessingcompletedcompleted
response_codenone202200200200
result_datanonenonenone"done""done"
Key Moments - 3 Insights
Why does the server respond with 202 Accepted instead of 200 OK immediately?
Because the task takes a long time, the server replies quickly with 202 to say it accepted the request and will process it later (see execution_table step 2).
How does the client know when the task is finished?
The client keeps polling the status URL until the server responds with status 'completed' and the result (see execution_table steps 4, 6).
What if the client stops polling too early?
Then the client won't get the final result because the task is still running (see execution_table step 4 shows 'processing').
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what status code does the server send immediately after receiving the start request?
A200
B202
C404
D500
💡 Hint
Check step 2 in the execution_table where the server responds to the start request.
At which step does the client receive the final result of the long-running task?
AStep 6
BStep 4
CStep 2
DStep 3
💡 Hint
Look for the step where the server responds with status 'completed' and the result in execution_table.
If the client stops polling after step 4, what will happen?
AClient gets the final result
BServer cancels the task
CClient never gets the final result
DServer sends an error
💡 Hint
See key_moments about polling and execution_table step 4 showing 'processing' status.
Concept Snapshot
Long-running operations use async responses.
Client sends request, server replies 202 Accepted immediately.
Server provides a status URL to check progress.
Client polls status URL until task completes.
Final result returned with 200 OK and data.
This avoids client waiting too long on one request.
Full Transcript
In long-running operations, the client sends a request to start a task. The server quickly replies with a 202 Accepted status and a Location header pointing to a status URL. The client then polls this URL repeatedly to check if the task is done. While the task runs, the server responds with status 'processing'. When finished, the server responds with status 'completed' and the final result. The client stops polling and uses the result. This approach prevents the client from waiting a long time on a single request and allows asynchronous processing.