简体   繁体   中英

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. 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:

If a resource has been created on the origin server, the response
SHOULD be 201 (Created) and contain an entity which describes the
status of the request and refers to the new resource, and a Location
header (see section 14.30 ).

To the best of my knowledge, you can only return a single location in the location header.

The aforementioned RFC2616 is outdated (thanks @Eric Stein). The updated semantics of POST can be found in 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
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).

While a POST MAY create multiple resources, it SHOULD only create a single root resource (or, at least, this is what the wording suggests).

Overall, there are only few hard limitations about what is and is not allowed, thus the wording of the 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.

POST is intended as a catch-all verb which permits the server to perform any action. 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 :

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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