Sometimes, tasks take a long time to finish. Long-running operations let the server start the task and tell you to check back later for the result.
Uploading a large file that takes minutes to process.
Starting a video conversion that needs time to complete.
Running a complex report that takes a while to generate.
Processing data that requires heavy calculations.
Any task where waiting for the response would make the app slow or unresponsive.
Syntax
Rest API
POST /start-task HTTP/1.1
Host: example.com
Response:
HTTP/1.1202 Accepted
Location: /task-status/12345
The server responds with status 202 to say it accepted the request but is still working.
The Location header gives a URL where you can check the task status later.
Examples
The client asks to convert a video. The server replies with 202 and a URL to check progress.
Rest API
POST /convert-video HTTP/1.1
Host: example.com
Response:
HTTP/1.1202 Accepted
Location: /status/convert-789
The client checks the status URL and sees the task is still running.
Rest API
GET /status/convert-789 HTTP/1.1
Host: example.com
Response:
HTTP/1.1200 OK
{
"status": "processing"
}
Later, the client checks again and sees the task is done with a link to the result.
Rest API
GET /status/convert-789 HTTP/1.1
Host: example.com
Response:
HTTP/1.1200 OK
{
"status": "done",
"result_url": "/videos/converted-789.mp4"
}
Sample Program
This example shows starting a report generation, then checking its status twice: once while it's running, and once when it's done with a download link.
Rest API
POST /start-report HTTP/1.1
Host: example.com
Response:
HTTP/1.1202 Accepted
Location: /report-status/555
---
GET /report-status/555 HTTP/1.1
Host: example.com
Response:
HTTP/1.1200 OK
{
"status": "in_progress"
}
---
GET /report-status/555 HTTP/1.1
Host: example.com
Response:
HTTP/1.1200 OK
{
"status": "completed",
"download_link": "/reports/report-555.pdf"
}
OutputSuccess
Important Notes
Always check the Location header to know where to poll for updates.
Use status codes like 202 to tell the client the task is accepted but not finished.
Polling too often can overload the server; use reasonable delays between checks.
Summary
Long-running operations let servers handle slow tasks without making clients wait.
Clients start the task and get a URL to check progress later.
This keeps apps responsive and user-friendly.
Practice
(1/5)
1. What is the main purpose of using long-running operations with async responses in REST APIs?
easy
A. To make the server process all requests instantly.
B. To let clients start a task and check its progress later without waiting.
C. To block the client until the task finishes.
D. To avoid using URLs for task status.
Solution
Step 1: Understand client-server interaction in async operations
Clients start a task and receive a URL to check progress later, avoiding waiting.
Step 2: Identify the benefit of async responses
This approach keeps apps responsive and user-friendly by not blocking clients.
Final Answer:
To let clients start a task and check its progress later without waiting. -> Option B
Quick Check:
Async responses = clients check progress later [OK]
Hint: Async means start task, check later URL [OK]
Common Mistakes:
Thinking server processes all requests instantly
Assuming client waits for task completion
Ignoring the use of status URLs
2. Which HTTP status code is typically returned immediately by a REST API to indicate a long-running operation has started asynchronously?
easy
A. 200 OK
B. 404 Not Found
C. 202 Accepted
D. 500 Internal Server Error
Solution
Step 1: Recall HTTP status codes for async start
202 Accepted means the request is accepted but processing is not complete yet.
Step 2: Compare with other codes
200 OK means completed, 404 means missing resource, 500 means server error.
Final Answer:
202 Accepted -> Option C
Quick Check:
Async start = 202 Accepted [OK]
Hint: 202 means accepted but not done yet [OK]
Common Mistakes:
Using 200 OK for async start
Confusing 404 with async status
Assuming 500 means task started
3. Given this REST API response after starting a long-running task: