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

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

问题描述

说我有一个API,该API公开了两个相关资源,即Company,该公司有很多员工.

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删除),则获取 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冲突.

从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天全站免登陆