What HTTP status code should be used for wrong input

131,013

Solution 1

We had the same problem when making our API as well. We were looking for an HTTP status code equivalent to an InvalidArgumentException. After reading the source article below, we ended up using 422 Unprocessable Entity which states:

The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415 (Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if an XML request body contains well-formed (i.e., syntactically correct), but semantically erroneous, XML instructions.

source: https://www.bennadel.com/blog/2434-http-status-codes-for-invalid-data-400-vs-422.htm

Solution 2

Codes starting with 4 (4xx) are meant for client errors. Maybe 400 (Bad Request) could be suitable to this case? Definition in http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html says:

"The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications. "

Solution 3

409 Conflict could be an acceptable solution.

According to: https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

The request could not be completed due to a conflict with the current state of the resource. This code is only allowed in situations where it is expected that the user might be able to resolve the conflict and resubmit the request. The response body SHOULD include enough information for the user to recognize the source of the conflict. Ideally, the response entity would include enough information for the user or user agent to fix the problem; however, that might not be possible and is not required.

The doc continues with an example:

Conflicts are most likely to occur in response to a PUT request. For example, if versioning were being used and the entity being PUT included changes to a resource which conflict with those made by an earlier (third-party) request, the server might use the 409 response to indicate that it can't complete the request. In this case, the response entity would likely contain a list of the differences between the two versions in a format defined by the response Content-Type.


In my case, I would like to PUT a string, that must be unique, to a database via an API. Before adding it to the database, I am checking that it is not already in the database.

If it is, I will return "Error: The string is already in the database", 409.

I believe this is what the OP wanted: an error code suitable for when the data does not pass the server's criteria.

Share:
131,013

Related videos on Youtube

Marek Sebera
Author by

Marek Sebera

Updated on July 08, 2022

Comments

  • Marek Sebera
    Marek Sebera over 1 year

    What is optimal HTTP response Code when not reporting 200 (everything OK) but error in input?

    Like, you submit some data to server, and it will response that your data is wrong

    using 500 looks more like Server Issue
    using 200 with warning/error response text is bad (allowing caching and everything is not OK)
    using 204 and returning nothing, is maybe good (but well supported?)
    using 404 is wrong if requested path (script) is available and in proper place

  • Keego
    Keego almost 7 years
    400 Bad Request isn't bad but should generally be reserved for malformed syntax. OP seems to be more concerned about a case with well-formed syntax but invalid values. Plus 400 is a fairly common "oh shit something wasn't right" response code which you might want to differentiate from a particular case of erroneous input.
  • Remy Lebeau
    Remy Lebeau almost 7 years
    The OP already ruled that out: "using 404 is wrong if requested path (script) is available and in proper place"
  • Calimo
    Calimo almost 5 years
    Note that the wording was updated in RFC 7231, and the "The request could not be understood by the server due to malformed syntax" was updated to "the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing)".

Related