Right HTTP status code to wrong input

前端 未结 6 1249
隐瞒了意图╮
隐瞒了意图╮ 2020-12-12 11:58

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 da

6条回答
  •  醉梦人生
    2020-12-12 12:38

    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.

提交回复
热议问题