How do I choose a HTTP status code in REST API for "Not Ready Yet, Try Again Later"?

HttpRestLanguage AgnosticBatch ProcessingHttp Status-Codes

Http Problem Overview


I'm developing a RESTful API in which http://server/thingyapi/thingyblob/1234 returns the file (aka "blob") associated with thingy #1234 to download. But it may be that the request is made at a time the file does not exist in the server but most definitely will be available at a later time. There's a batch process in the server that generates all the blobs for all the thingies. Thingy 1234 already exists and its data, other than the blob, is already available. The server hasn't got to generating thingy 1234's blob yet.

I don't want to return 404; that's for thingies that do not exist. This is a thingy that exists, but its blob hasn't been generated yet. Kinda like a YouTube video that's "processing." I don't think redirection codes would be proper either; there's no "other" URL to try.

What's the correct HTTP status code to return in such a case?

Http Solutions


Solution 1 - Http

I suggest 202 - Accepted. From the documentation:

> The request has been accepted for processing, but the processing has not been completed. [...] Its purpose is to allow a server to accept a request for some other process (perhaps a batch-oriented process that is only run once per day)

Solution 2 - Http

The "problem", such as it is, is on the server side: the client has made a well formed request, but the server can not satisfy it. So I'm inclined to a "Server Error", 5xx status code.

Quoth RFC 7231 (the current HTTP standard, emphasis added): > The 5xx (Server Error) class of status code indicates that the server is aware that it has erred or is incapable of performing the requested method. Except when responding to a HEAD request, the server SHOULD send a representation containing an explanation of the error situation, and whether it is a temporary or permanent condition.

Note

  • "erred or is incapable of performing the request": despite their title of "Server Error", they are not just for server errors.
  • "temporary or permanent": these codes are suitable for temporarily unavailable resources, like yours.

Of the available codes, I'd say 503, "Service Unavailable" was the best fit:

> The 503 (Service Unavailable) status code indicates that the server is currently unable to handle the request due to a temporary overload or scheduled maintenance, which will likely be alleviated after some delay. The server MAY send a Retry-After header field... to suggest an appropriate amount of time for the client to wait before retrying the request.

Note:

  • "likely be alleviated after some delay": true for your case.
  • "temporary overload": not pedantically true for your case. But, it could be argued, were your server much faster, the batch processing would have already been done when the client made the request, so it is a kind of "overload": the client is asking for resources faster than the server can make them available.
  • Retrying is suitable for your service, so your reply ought to include a Retry-After value. You could provide as the value the estimated completion time of the next execution of the batch process, or the execution interval of the batch process.

Defining your own 5xx status code (591, for example), although permitted, would have the wrong semantics: > a client MUST understand the class of any status code, as indicated by the first digit, and treat an unrecognized status code as being equivalent to the x00 status code of that class

Clients would treat your own status code as 500, "Internal Server Error", which would not be right.

Solution 3 - Http

I think that 423 - Locked can be used for this purpose:

> The 423 (Locked) status code means the source or destination resource of a method is locked. This response SHOULD contain an appropriate precondition or postcondition code, such as 'lock-token-submitted' or 'no-conflicting-lock'.

Solution 4 - Http

> I don't want to return 404; that's for thingies that do not exist.

The URL doesn't correspond to a request for a thingy.

http://server/thingyapi/thingyblob/1234

The client is requesting a thingyblob, which doesn't exist. If it existed, you would give it to them.

404.

Solution 5 - Http

Another option: 503 - Service Unavailable.

Solution 6 - Http

Since your resource is not ready, you probably know when (approximately) it will be available and when client may retry his request. This means you might want to utilize Retry-After header. This header is valid with 503 (Service Unavailable), which means whole site is down for maintenance, and 3xx (Redirection) responses.

In my opinion 302 (Found) with Retry-After header would be the best option, but I am not sure if Location field of response header can be equal to request url. It's circular redirect, anyway.

Solution 7 - Http

409 Conflict

Indicates that the request could not be processed because of conflict in the request, such as an edit conflict in the case of multiple updates. [Source Wikipedia.]

This could be appropriate.

If you cant fulfill the request by returning the data - then its not a success. I think 202 suggests the server has queued the request and it will fulfill the request later. But in your case, the request is for data now and has failed. If you retry later it is a different request.

I think you have a conflict.. you want the data.. but its being edited / updated. This would also be the case if Thingy1234 already existed and had been successfully downloaded before, but now was in the process of being edited was was unavailable whilst the edit was taking place.

Solution 8 - Http

501 - Not Implemented

Exactly like how it sounds. A feature that is not yet implemented, but implies future availability.

Here is a link to a summary of 5xx errors.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJCCyCView Question on Stackoverflow
Solution 1 - HttpmatsevView Answer on Stackoverflow
Solution 2 - HttpRaedwaldView Answer on Stackoverflow
Solution 3 - HttpFernando OrtegaView Answer on Stackoverflow
Solution 4 - HttpfunrollView Answer on Stackoverflow
Solution 5 - HttpBrian KellyView Answer on Stackoverflow
Solution 6 - HttpskaleeView Answer on Stackoverflow
Solution 7 - HttpJ MooreView Answer on Stackoverflow
Solution 8 - HttpDanView Answer on Stackoverflow