我应该为UPDATE
(PUT
)和DELETE
(例如产品成功更新)设置什么状态码?
最佳答案
对于PUT 请求:HTTP 200,HTTP 204 应该意味着“资源更新成功”。 HTTP 201 如果 PUT 请求创建了新资源。
对于DELETE 请求:HTTP 200 或HTTP 204 应表示“资源已成功删除”。
HTTP 202 也可以通过任一操作返回,这意味着该指令已被服务器接受,但尚未完全应用。操作稍后可能会失败,因此客户端不应完全假设操作成功。
客户端收到它无法识别的状态代码,但它以 2 开头,应将其视为 200 OK。
If an existing resource is modified, either the 200 (OK) or 204 (No Content) response codes SHOULD be sent to indicate successful completion of the request.
A successful response SHOULD be 200 (OK) if the response includes an entity describing the status, 202 (Accepted) if the action has not yet been enacted, or 204 (No Content) if the action has been enacted but the response does not include an entity.
来源:IETF: RFC-9110 Method Definitions
HTTP 200 OK: Standard response for successful HTTP requests. The actual response will depend on the request method used.
HTTP 204 No Content: The server successfully processed the request, but is not returning any content
关于用于更新和删除的 HTTP 状态代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2342579/