How do I choose a HTTP status code in REST API for

2019-01-21 06:13发布

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?

8条回答
放荡不羁爱自由
2楼-- · 2019-01-21 06:34

Another option: 503 - Service Unavailable.

查看更多
做个烂人
3楼-- · 2019-01-21 06:35

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.

查看更多
Juvenile、少年°
4楼-- · 2019-01-21 06:38

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.

查看更多
淡お忘
5楼-- · 2019-01-21 06:48

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.

查看更多
祖国的老花朵
6楼-- · 2019-01-21 06:52

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 the standard:

Response status codes beginning with the digit "5" indicate cases in which the server is aware that it has erred or is incapable of performing the request... the server SHOULD include an entity [indicating] 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 server is currently unable to handle the request due to a temporary overloading or maintenance of the server. The implication is that this is a temporary condition which will be alleviated after some delay. If known, the length of the delay MAY be indicated in a Retry-After header. If no Retry-After is given, the client SHOULD handle the response as it would for a 500 response.

Note:

  • "a temporary condition which will be alleviated after some delay": true for your case.
  • "temporary overloading": 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 "overloading": the client is asking for resources faster than the server can make them available.
  • "the client SHOULD handle the response as it would for a 500 response.": which is "Internal Server Error", the kind of response when the server fails because of a bug. The standard seems to be implying that a well behaved client should not retry in this case. 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:

HTTP status codes are extensible... applications MUST understand the class of any status code, as indicated by the first digit, and treat any unrecognized response as being equivalent to the x00 status code of that class

Clients would treat your own status code as 500, "Internal Server Error", which (as I discussed above) would not be right.

查看更多
Bombasti
7楼-- · 2019-01-21 06:54

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'.

查看更多
登录 后发表回答