I am implementing a REST API which involves creating a object on the server. The object creation involves multiple steps and may take a while. I do not want the user to wait on it. I simply return a 202 response with a unique request id for the client request and start some threads on the server to create the object. The client is supposed to check back to see whether the request is completed or not, in the future. The flow goes like this:
/my-app/<reqId>
/my-app/<reqId>
Now at the third step, these things might happen:
Now what http code should my API /my-app/<reqId>
respond for the above three scenarios?
The HTTP 204 No Content success status response code indicates that a request has succeeded, but that the client doesn't need to navigate away from its current page. This might be used, for example, when implementing "save and continue editing" functionality for a wiki site.
The HTTP 201 Created success status response code indicates that the request has succeeded and has led to the creation of a resource.
The HTTP 200 OK success status response code indicates that the request has succeeded. A 200 response is cacheable by default. The meaning of a success depends on the HTTP request method: GET : The resource has been fetched and is transmitted in the message body.
The first GET request received for the resource starts the computation on the server. If the computation completes within a few seconds, the computed representation is returned. Otherwise, a 202 "Accepted" status code is returned, and the client must poll the resource until the final representation is available.
I might do it a bit differently from the start. The Location
header has a specific meaning, pointing to the actual resource connected to the request, basically the "result" of whatever was requested, not the resource indicating the state of the request itself. This might be a small difference, might nonetheless be confusing later.
Also the specification says the 202
should return content indicating or linking to the "state" resource that describes the progress of the request itself.
So the flow might be:
POST
202 Accepted
. Location
header points to the URI where the requested resource will be (this is not the state), this will be 404
until the processing is done. Also, the content of the 202
might include the "state" representation. The Content-Location
header has the link to this "state" resource.GET
s the state resource to check on the progress. This resource always exists, so it always returns 200
.Location
now exists, otherwise it will never exist. State resource continues to exists indefinitely.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With