问题描述
说我有一个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:
它与您的情况不完全匹配,但是恕我直言.
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错误代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!