What is the appropriate HTTP status code to ask a client to redo an operation

376 Views Asked by At

I have an endpoint on my web application that takes data from a client and once enough data is collected, an operation is performed. If the result of this operation is invalid, I need to inform the client that this operation must be redone.

I could send a response with some sort of flag in it, but if a status code exists for this purpose already, I'd rather utilize it.

Looking at the definitions of the status codes here, it seems that there is not one that is appropriate, however, if I just take the names into account and not the descriptions, the status code 406 Not Acceptable sounds the most appropriate.

1

There are 1 best solutions below

1
On

406 Not Acceptable is not appropriate because the status code is for content negotiation:

The 406 (Not Acceptable) status code indicates that the target resource does not have a current representation that would be acceptable to the user agent, according to the proactive negotiation header fields received in the request (Section 5.3), and the server is unwilling to supply a default representation. -- https://www.rfc-editor.org/rfc/rfc7231#section-6.5.6

202 Accepted seems to be better:

The 202 (Accepted) status code indicates that the request has been accepted for processing, but the processing has not been completed. -- https://www.rfc-editor.org/rfc/rfc7231#section-6.3.3

... But I am not sure that the status code's purpose matches yours:

The 202 response is intentionally noncommittal. 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. -- https://www.rfc-editor.org/rfc/rfc7231#section-6.3.3

Usually http responses does not explicitly request user agent to send additional information. The only status code to do that I know is 401 Unauthorized which requests Authorization header. You may need to design your own status code for your purpose. Please consult with people at HTTP Working Group mailing list.