REST HTTP status codes for failed validation or invalid duplicate

前端 未结 9 1752
借酒劲吻你
借酒劲吻你 2020-11-22 13:53

I am building an application with a REST-based API and have come to the point where I am specifying status codes for each requests.

What status code should i send for

9条回答
  •  渐次进展
    2020-11-22 14:09

    Status Code 304 Not Modified would also make an acceptable response to a duplicate request. This is similar to processing a header of If-None-Match using an entity tag.

    In my opinion, @Piskvor's answer is the more obvious choice to what I perceive is the intent of the original question, but I have an alternative that is also relevant.

    If you want to treat a duplicate request as a warning or notification rather than as an error, a response status code of 304 Not Modified and Content-Location header identifying the existing resource would be just as valid. When the intent is merely to ensure that a resource exists, a duplicate request would not be an error but a confirmation. The request is not wrong, but is simply redundant, and the client can refer to the existing resource.

    In other words, the request is good, but since the resource already exists, the server does not need to perform any further processing.

提交回复
热议问题