带有 mysql id 的 PUT 的 HTTP 状态代码。
HTTP Status code for PUT with mysql id.
我们有一个 PUT 端点,它使用自动递增的 MYSQL id 更新一行。如果我们在请求正文中指定 id,就像更改 id 值一样,端点不会更改 id(这是正确的行为)。但它也是 returns 200 因为技术上没有验证失败。这个 return 应该是 200 吗?还是应该是 400 或 403?
If we specify the id in the body of the request, as in change the id value, the endpoint does not change the id (which is right behavior).
如果有效负载中收到的 ID 与数据库中存储的 ID 匹配并且更新成功,服务应该 return successful status code such as 204
or 200
.
另一方面,如果有效载荷中收到的 ID 与数据库中存储的 ID 不匹配,我会理解 client error. And 409
似乎是一个 合理的选择:用于表示请求与服务器上资源的当前状态冲突。有效负载中的 ID 与数据库中的 ID 不匹配 存在冲突。
看看 409
status code is defined in the RFC 7231:
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. [...]
响应应包括所有必要的信息,以便客户端识别冲突的来源,然后能够重新提交请求。对于在 Web API 中报告问题,我建议您检查 RFC 7807.
我们有一个 PUT 端点,它使用自动递增的 MYSQL id 更新一行。如果我们在请求正文中指定 id,就像更改 id 值一样,端点不会更改 id(这是正确的行为)。但它也是 returns 200 因为技术上没有验证失败。这个 return 应该是 200 吗?还是应该是 400 或 403?
If we specify the id in the body of the request, as in change the id value, the endpoint does not change the id (which is right behavior).
如果有效负载中收到的 ID 与数据库中存储的 ID 匹配并且更新成功,服务应该 return successful status code such as 204
or 200
.
另一方面,如果有效载荷中收到的 ID 与数据库中存储的 ID 不匹配,我会理解 client error. And 409
似乎是一个 合理的选择:用于表示请求与服务器上资源的当前状态冲突。有效负载中的 ID 与数据库中的 ID 不匹配 存在冲突。
看看 409
status code is defined in the RFC 7231:
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. [...]
响应应包括所有必要的信息,以便客户端识别冲突的来源,然后能够重新提交请求。对于在 Web API 中报告问题,我建议您检查 RFC 7807.