由于父实体消失而无法创建新资源的 HTTP 错误代码是什么 [英] What HTTP error code for failure to create a new resource because a parent entity is gone

查看:22
本文介绍了由于父实体消失而无法创建新资源的 HTTP 错误代码是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个 API 公开了两个相关资源,即拥有许多员工的公司.

Say i have an API exposing two related resources, Company which has many Employees.

假设我创建了一个新公司:POST http://domain/api/company/ 返回一些东西像 http://domain/api/company/123.

Say I create a new Company: POST http://domain/api/company/ which returns something like http://domain/api/company/123.

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

If company/123 is removed from the system (say by a DELETE) then GET http://domain/api/company/123 could return HTTP response code 410 (Gone).

我的问题是这个.如果我现在尝试通过执行 POST http://domain/api/employees/(请求正文中companyId设置为123)请求无效,服务器返回什么HTTP响应码?

My question is this. If I now try to create an Employee under Company/123 by doing POST http://domain/api/employees/ (with companyId set to 123 in the request body) what HTTP response code should be sent back by the server due to the invalid request?

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

E.g. the request is correctly formated, but there is a logical error due to the fact that company 123 is gone.

内部服务器错误 500?

Internal Server Error 500?

推荐答案

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

Not a 500, because there is no problem with the server.

我建议使用 409 Conflict.

I would suggest 409 Conflict.

来自 RFC:

由于与资源的当前状态冲突,请求无法完成.仅在预期用户可能能够解决冲突并重新提交请求的情况下才允许使用此代码.响应主体应该包含足够的信息让用户识别冲突的来源.理想情况下,响应实体应包含足够的信息供用户或用户代理解决问题;然而,这可能是不可能的,也不是必需的.响应 PUT 请求时最有可能发生冲突.例如,如果正在使用版本控制并且被 PUT 的实体包含对资源的更改,该更改与之前(第三方)请求所做的更改发生冲突,则服务器可能会使用 409 响应来指示它无法完成请求.在这种情况下,响应实体可能会以响应 Content-Type 定义的格式包含两个版本之间差异的列表.

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.

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

It doesn't exactly match your case, but it is very close IMHO.

例如,服务器可能会告诉您父资源不存在供您发布到,并且您可以重新提交"员工到不同的公司.:)

For example the server could tell you the parent resource does not exist for you to post to, and you can "resubmit" the employee to a different company. :)

这篇关于由于父实体消失而无法创建新资源的 HTTP 错误代码是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆