How can I return a 202 Accepted response for long running REST call?
See original GitHub issueHope this is the right place for a question.
I have a REST API post request that does a lot of computation, /crunch
. Rather than block the event loop I would like /crunch
to return 202 Accepted
status code along with a token string. Then the user of the API can call get request /result/{token}
to check on the status of the computation. This is outlined nicely here for example.
Is it possible to modify the response status code, for example, similar to this approach in Sanic?
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Long running ReST requests and status endpoints - Steve Dunn
Some of it says that the polling endpoint should return 202 Accepted until the resource is ready. One such page is on the...
Read more >HTTP Status 202 (Accepted) - REST
HTTP Status 202 indicates that the request has been accepted for processing, but the processing has not been completed. This status code is ......
Read more >REST API Best Practices — Decouple Long-running Tasks ...
Queue up the long-running task requested in a message broker. Respond to the user immediately so they can get back to their busy...
Read more >When returning a `202` containing a status endpoint, what ...
This page covers the you should return a 202 for long running stuff, but doesn't cover what the status endpoint should return. rest....
Read more >202 Accepted - HTTP - MDN Web Docs
The HyperText Transfer Protocol (HTTP) 202 Accepted response status code indicates that the request has been accepted for processing, ...
Read more >
Top Related Medium Post
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@danieljfarrell here’s a bit of history, now in the docs: https://fastapi.tiangolo.com/history-design-future/
And I quoted you 😁
@danieljfarrell Its really easy to modify the response code to suit your needs. In the example below, I use
/jobs
to post new jobs to. This returns aHTTP_201_CREATED
on submit. You can then take the id that it generates and callGET /jobs/{id}
to check the status, along with itsHTTP_202_ACCEPTED
code 🎉. Note: I’m using deque’s because they’re thread-safe, but I haven’t thoroughly tested this out and haven’t implemented task switching/moving jobs around to different queues. Hope it helps!Submitting a new job, getting its id
Getting the status for a job, given its id