gpt4 book ai didi

api - 由于父实体消失而无法创建新资源的 HTTP 错误代码是什么

转载 作者:可可西里 更新时间:2023-11-01 15:09:56 26 4
gpt4 key购买 nike

假设我有一个 API 公开两个相关资源,公司有很多员工。

假设我创建了一个新公司:POST http://domain/api/company/它返回类似 http://domain/api/company/123 的内容.

如果 company/123 从系统中删除(比如通过 DELETE),那么 GET http://domain/api/company/123可能会返回 HTTP 响应代码 410(消失)。

我的问题是这样的。如果我现在尝试通过执行 POST http://domain/api/employees/ 在 Company/123 下创建员工(请求正文中的 companyId 设置为 123)由于请求无效,服务器应该发回什么 HTTP 响应代码?

例如请求格式正确,但由于公司 123 已不存在,因此存在逻辑错误。

内部服务器错误 500?

最佳答案

不是500,因为服务器没有问题。

我会建议 409 冲突。

来自 RFC:

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. Conflicts are most likely to occur in response to a PUT request. For example, if versioning were being used and the entity being PUT included changes to a resource which conflict with those made by an earlier (third-party) request, the server might use the 409 response to indicate that it can't complete the request. In this case, the response entity would likely contain a list of the differences between the two versions in a format defined by the response Content-Type.

它不完全符合您的情况,但恕我直言,它非常接近。

例如,服务器可能会告诉您父资源不存在供您发布,您可以将员工“重新提交”到另一家公司。 :)

关于api - 由于父实体消失而无法创建新资源的 HTTP 错误代码是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7088414/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com