REST API 设计查询 2
REST API Design Query 2
POST操作是否应该一次只支持创建一个资源或者它可以接受在单个POST请求中创建的资源集合?
从 definition of POST in RFC2616 可以推断,应该只创建一个资源(如 RFC 中定义的)。首先,该部分在整个段落中谈论“创建的实体”(单数)。更明显的部分是通过 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).
据我所知,您只能 return 位置 header 中的一个位置。
前面提到的 RFC2616 已经过时(感谢@Eric Stein)。 updated semantics of POST can be found in RFC7231。同样,推荐的响应提示语义:
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 可以创建多个资源,但它应该只创建一个根资源(或者,至少,这是措辞所建议的)。
总的来说,对于什么是允许的和什么是不允许的只有很少的硬性限制,因此 RFC 的措辞(应该、可以、...)。只要您在相应的 RFC 中没有看到 MUST 或 MUST NOT,您就可以随心所欲,但可能会违反最佳实践。
POST
旨在用作允许服务器执行任何操作的包罗万象的动词。虽然它最常用于创建单个资源,但它当然也可以创建任意数量的资源。这在更新后的 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操作是否应该一次只支持创建一个资源或者它可以接受在单个POST请求中创建的资源集合?
从 definition of POST in RFC2616 可以推断,应该只创建一个资源(如 RFC 中定义的)。首先,该部分在整个段落中谈论“创建的实体”(单数)。更明显的部分是通过 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).
据我所知,您只能 return 位置 header 中的一个位置。
前面提到的 RFC2616 已经过时(感谢@Eric Stein)。 updated semantics of POST can be found in RFC7231。同样,推荐的响应提示语义:
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 可以创建多个资源,但它应该只创建一个根资源(或者,至少,这是措辞所建议的)。
总的来说,对于什么是允许的和什么是不允许的只有很少的硬性限制,因此 RFC 的措辞(应该、可以、...)。只要您在相应的 RFC 中没有看到 MUST 或 MUST NOT,您就可以随心所欲,但可能会违反最佳实践。
POST
旨在用作允许服务器执行任何操作的包罗万象的动词。虽然它最常用于创建单个资源,但它当然也可以创建任意数量的资源。这在更新后的 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).