Most appropriate HTTP status code for “job in prog

2019-04-21 18:40发布

问题:

What is the most appropriate HTTP status code to give to a client to mean "your request is fine, but it is still in progress; check back shortly in the exact same place."

For example, say the client submits an initial request to start a heavy query, and the server immediately returns a URL that the client can poll periodically for the result. In the case the client calls this URL before the job is completed, what is the most appropriate HTTP status code to return?

202 Accepted would be my first impulse. Is this the best one, or is there a better one that is more idiomatic for this purpose in REST interfaces?

回答1:

To me, 202 Accepted would be the best way to go.

See the documentation on the W3C website.

10.2.3 202 Accepted

The request has been accepted for processing, but the processing has not been completed. The request might or might not eventually be acted upon, as it might be disallowed when processing actually takes place. There is no facility for re-sending a status code from an asynchronous operation such as this.

The 202 response is intentionally non-committal. 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) without requiring that the user agent's connection to the server persist until the process is completed. The entity returned with this response SHOULD include an indication of the request's current status and either a pointer to a status monitor or some estimate of when the user can expect the request to be fulfilled.