简体   繁体   English

REST API设计查询2

[英]REST API Design Query 2

应该张贴只有一个资源的操作支持创建在同一时间也可以在一个单一的POST请求接受创作资源的集合?

From the definition of POST in RFC2616 , it can be inferrered that only a single resource SHOULD (as defined in the RFC) be created. 根据RFC2616中POST定义 ,可以推断出仅应创建一个资源(如RFC中所定义)。 For one, the section talks about " the created entity " (singular) throughout the paragraph. 首先,本节在整个段落中讨论“ 创建的实体 ”(单数)。 The more obvious section is the recommended response for when new resource(s) are created through a POST: 最明显的部分是通过POST创建新资源时的建议响应:

If a resource has been created on the origin server, the response 如果在原始服务器上创建了资源,则响应
SHOULD be 201 (Created) and contain an entity which describes the 应该为201(创建),并包含描述
status of the request and refers to the new resource, and a Location 请求的状态并引用新资源以及一个位置
header (see section 14.30 ). 标头(请参见第14.30节)。

To the best of my knowledge, you can only return a single location in the location header. 据我所知,您只能在location标头中返回一个位置。

The aforementioned RFC2616 is outdated (thanks @Eric Stein). 前面提到的RFC2616已过时(感谢@Eric Stein)。 The updated semantics of POST can be found in RFC7231 . POST更新语义可以在RFC7231中找到 Again, the recommended response hints to the semantics: 再次,建议的响应暗示了语义:

If one or more resources has been created on the origin server as a 如果已在原始服务器上将一个或多个资源创建为
result of successfully processing a POST request, the origin server 成功处理POST请求的结果,原始服务器
SHOULD send a 201 (Created) response containing a Location header 应该发送包含Location标头的201(已创建)响应
field that provides an identifier for the primary resource created 为创建的主要资源提供标识符的字段
( Section 7.1.2 ) and a representation that describes the status of the 第7.1.2节 )以及描述状态的表示。
request while referring to the new resource(s). 请求,同时引用新资源。

While a POST MAY create multiple resources, it SHOULD only create a single root resource (or, at least, this is what the wording suggests). 当POST可以创建多个资源时,它应该只创建一个根资源(或者,至少,这是措辞所暗示的)。

Overall, there are only few hard limitations about what is and is not allowed, thus the wording of the RFC (SHOULD, MAY, ...). 总体而言,关于什么是什么以及什么是不允许的,几乎没有硬性限制,因此RFC的措辞(SHOULD,MAY等)。 As long as you do not see a MUST or MUST NOT in the corresponding RFC, you are free to do as you wish, but may violate best practices. 只要您在相应的RFC中看不到MUST或MUST NOT,就可以随意进行操作,但是可能会违反最佳做法。

POST is intended as a catch-all verb which permits the server to perform any action. POST旨在作为一个包罗万象的动词,允许服务器执行任何操作。 While it is most often used to create a single resource, it is certainly within the spec for it to create any number of resources. 尽管它最常用于创建单个资源,但它在规范内肯定可以创建任意数量的资源。 This is clarified in the updated RFC 7231 : 在更新的RFC 7231中对此进行了澄清:

If one or more resources has been created on the origin server as a result of successfully processing a POST request, the origin server SHOULD send a 201 (Created) response containing a Location header field that provides an identifier for the primary resource created ( Section 7.1.2 ) and a representation that describes the status of the request while referring to the new resource(s). 如果由于成功处理POST请求而在原始服务器上创建了一个或多个资源,则原始服务器应发送201(已创建)响应,该响应包含一个Location头字段,该字段为创建的主要资源提供标识符( 第7.1节) .2 )以及在引用新资源时描述请求状态的表示。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM