放在 REST 集合上。我提供地址吗?

PUT on REST collection. Do I provide addresses?

当您对 REST 集合执行 PUT 时,您是否应该提供集合中成员的地址?

PUT 给指定地址的狗

[{"name":"sparky", "id":1}, {"name":"rusty", "id":2}]

或...

PUT 未指定地址的狗

[{"name":"sparky"}, {"name":"rusty"}]

并让服务器 return 集合中新成员的位置。在我的例子中,我的 table.

中的行 ID

如果您要发送 PUT 来替换整个集合中的每个资源,您应该发送完整的新/更新实体。

  • 如果id是实体的一部分,你应该在实体内发送它,但对于与URI无关的客户端(a.k.a "address" ) 元素,因为 客户端现在知道服务器如何构建 URI。
  • 如果 id 不是客户端实体的一部分,您不应在实体内发送它。

但这两种情况都是可能的,您的服务器可以接受这两种情况,重要的是 PUT 集合的行为与 HTTP/1.1 specs 中描述的一样:

The PUT method requests that the enclosed entity be stored under the supplied Request-URI.

因此,如果您 PUT/api/animals/dogs/ 上,狗只会在 /dogs/ 下找到,而不是例如/cats/

If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server

这意味着任何集合上的 PUT 会删除整个集合并使用方法调用正文中给定的实体创建一个新集合。

If the Request-URI does not point to an existing resource [...] the origin server can create the resource with that URI.

这意味着如果 /animals/dogs/ 为空,您应该在那里创建一个新集合。