无法处理请求的 HTTP 状态代码
HTTP status code for inability to process request
向 /api/ingredients/123
端点发出 delete
请求。
如果成分 123 不存在,我希望我应该 return 404 Not Found 状态代码。同意吗?
如果成分 123 确实存在,但在现有配方中使用,因此无法删除怎么办。应该 returned 什么状态码?
304 未修改,也许?或者,412 前提条件失败。
这里有一个状态码定义列表:https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
我认为这个问题的任何答案都是 30% 的技术,70% 的意见。
回复:响应成分 123 不存在 [并且从未存在] 的 HTTP 状态代码;是的,我同意 404 - Not Found - 是正确的。
如果成分 123 确实存在并且现在被删除,那么 410 - Gone - 是合适的。 (我的网站对过时的产品使用 410 状态代码。)
但是,您的案例中已有成分 123,但由于数据依赖性冲突 ("an existing recipe"),无法处理针对该特定成分的删除请求。对于这种情况,405 - 方法不允许 - 是合适的。
405 的 HTTP 状态表示成分参考正常,但不是删除请求。
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.
在您的情况下,无法删除一种成分,因为它在一个或多个食谱中使用(资源的冲突状态)。但是一旦该成分不再使用,它就可以被删除。
向 /api/ingredients/123
端点发出 delete
请求。
如果成分 123 不存在,我希望我应该 return 404 Not Found 状态代码。同意吗?
如果成分 123 确实存在,但在现有配方中使用,因此无法删除怎么办。应该 returned 什么状态码?
304 未修改,也许?或者,412 前提条件失败。
这里有一个状态码定义列表:https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
我认为这个问题的任何答案都是 30% 的技术,70% 的意见。
回复:响应成分 123 不存在 [并且从未存在] 的 HTTP 状态代码;是的,我同意 404 - Not Found - 是正确的。
如果成分 123 确实存在并且现在被删除,那么 410 - Gone - 是合适的。 (我的网站对过时的产品使用 410 状态代码。)
但是,您的案例中已有成分 123,但由于数据依赖性冲突 ("an existing recipe"),无法处理针对该特定成分的删除请求。对于这种情况,405 - 方法不允许 - 是合适的。
405 的 HTTP 状态表示成分参考正常,但不是删除请求。
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.
在您的情况下,无法删除一种成分,因为它在一个或多个食谱中使用(资源的冲突状态)。但是一旦该成分不再使用,它就可以被删除。