当正文中的 ID 与路径中的 ID 不匹配时,正确的 HTTP 状态代码?
Correct HTTP status code for when ID in the body does not match ID in the path?
假设我 PUT 到 /api/resource/50
并且在正文中我有 {"Id": 12, ...}
是否有针对此不匹配的 4xx HTTP 状态?我在想这个错误的方式吗?现在我只返回 400.
我可能会选择代码 409(冲突)。
这个维基百科页面非常方便:
4xx Status Codes
409
冲突怎么办?
The 409
(Conflict) status code indicates that the request could not
be completed due to a conflict with the current state of the target
resource. This code is used in situations where the user might be
able to resolve the conflict and resubmit the request. The server
SHOULD generate a payload that includes enough information for a user
to recognize the source of the conflict. [...]
但是使用400
应该不是问题。只需确保您 return 响应负载中有关错误的一些详细信息。
假设我 PUT 到 /api/resource/50
并且在正文中我有 {"Id": 12, ...}
是否有针对此不匹配的 4xx HTTP 状态?我在想这个错误的方式吗?现在我只返回 400.
我可能会选择代码 409(冲突)。
这个维基百科页面非常方便:
4xx Status Codes
409
冲突怎么办?
The
409
(Conflict) status code indicates that the request could not be completed due to a conflict with the current state of the target resource. This code is used in situations where the user might be able to resolve the conflict and resubmit the request. The server SHOULD generate a payload that includes enough information for a user to recognize the source of the conflict. [...]
但是使用400
应该不是问题。只需确保您 return 响应负载中有关错误的一些详细信息。