简体   繁体   English

如果发布子资源时资源不存在,则在REST API中使用哪个HTTP状态代码?

[英]Which HTTP status code to use in a REST API if the resource doesn't exist when POSTing a subresource?

To create a document for a project in an REST API, 要在REST API中为项目创建文档,

POST /projects/1234/documents

What status is expected to return if the project with id=1234 doesn't exist? 如果id = 1234的项目不存在,预期返回什么状态?


Related to this question , but dealing with POSTs, not GETs. 这个问题有关 ,但是处理POST,而不是GET。

Short answer: for most use cases similar to what you describe, 404. 简短答案:对于大多数与您描述的情况类似的用例,404。

More completely: the notion of "subresource" doesn't really exist in HTTP. 更完整的说:“子资源”的概念在HTTP中并不存在。

Yes, URI have a standard hierarchy of segments, and you can use relative resolution of dot segments to compute one URI from another. 是的,URI具有段的标准层次结构,并且您可以使用点段的相对分辨率来计算另一个URI。 Those semantics do not translate into a hierarchy of resources. 这些语义不会转换为资源层次结构。

/projects/1234/documents isn't a description of a path traversal, it's a key into a dictionary. /projects/1234/documents不是路径遍历的描述,它是字典的关键。

As far as HTTP is concerned, it is perfectly fine that /projects/1234/documents exists even though /projects and /projects/1234 do not. 就HTTP而言,即使/projects/projects/1234不存在, /projects/1234/documents存在也很好。

Put slightly differently, the metadata in your response, which includes the status code, apply to the /projects/1234/documents resource, and not anything else . 稍有不同,响应中的元数据(包括状态代码)适用于/projects/1234/documents资源, 而不适用于其他任何内容

Note that this goes both directions: not only does /projects/1234/documents not imply anything about /projects/1234 , but it is also the case that it doesn't imply anything about /projects/1234/documents/5678 . 请注意,这是双向的: /projects/1234/documents不暗含关于/projects/1234任何信息,但也有可能不暗含关于/projects/1234/documents/5678任何信息。

Your best reference for getting your head around this is still going to be Fielding's thesis . 最好的参考仍然是Fielding的论文 It may also help to review RFC 7234 审查RFC 7234也可能有帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果父资源不存在,在REST API中使用哪个HTTP代码用于子资源? - Which HTTP code to use, in a REST API, for a subresource if the parent resource doesn't exist? 哪个HTTP代码用于REST API中的空子资源? - Which HTTP code to use for an empty subresource in a REST API? REST API用户尝试访问对象时使用哪个HTTP状态代码并出现错误? - Which HTTP status code to use when REST API user is trying to acces an object and there is an error? What HTTP status code is more appropriate to return from a REST API PUT/PATCH method when a resource is in a state where it can't be updated? - What HTTP status code is more appropriate to return from a REST API PUT/PATCH method when a resource is in a state where it can't be updated? 与不存在的项目相关的验证失败的Http状态代码 - Http Status Code For Validation Failure Related To An Item Which Doesn't Exist REST API-将资源设计为主资源和子资源 - REST API - Design a resource as a primary and subresource 当REST中的资源不支持操作时,HTTP状态代码404或501 - HTTP Status Code 404 or 501 when Operation is not supported by a Resource in REST REST API - 哪个 Http 状态代码用于预订 ZDB974238714CA8DE6434A7CE1DZ83? - REST API - Which Http status codes to use for a booking API? 哪个HTTP重定向状态代码最适合此REST API方案? - Which HTTP redirect status code is best for this REST API scenario? REST API和HTTP状态代码 - REST API & HTTP Status Code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM