Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP status code for async tasks

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:

  1. The client POSTs the object.
  2. The server responds with a 202 Accepted code with a Location header /my-app/<reqId>
  3. The client does a GET on /my-app/<reqId>

Now at the third step, these things might happen:

  1. Object creation is still in progress(client should check again after sometime).
  2. Some Error occured.
  3. Object is successfully created.

Now what http code should my API /my-app/<reqId> respond for the above three scenarios?

like image 425
falcon Avatar asked Apr 05 '16 09:04

falcon


People also ask

What is a 204 status code?

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.

What is a 201 response code?

The HTTP 201 Created success status response code indicates that the request has succeeded and has led to the creation of a resource.

What is a 200 response code?

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.

How do you handle a 202 response?

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.


1 Answers

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:

  1. Client does POST
  2. Server sends 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.
  3. Client GETs the state resource to check on the progress. This resource always exists, so it always returns 200.
  4. If the state indicates success, the resource indicated in the Location now exists, otherwise it will never exist. State resource continues to exists indefinitely.
like image 135
Robert Bräutigam Avatar answered Nov 09 '22 12:11

Robert Bräutigam