[英]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.