在 URL 中表示资源克隆操作的宁静方式是什么? [英] What is the restful way to represent a resource clone operation in the URL?
问题描述
我有一个 REST API,它公开了一个复杂的大型资源,我希望能够克隆这个资源.假设资源暴露在 /resources/{resoureId}
I have REST API that exposes a complex large resource and I want to be able to clone this resource. Assume that the resource is exposed at /resources/{resoureId}
要克隆资源 10,我可以做类似的事情.
To clone resource 10 I could do something like.
GET/resources/10
POST/resources/
put 主体包含GET/resources/10
表示的副本,没有 id 以便POST
创建一个新资源.
GET /resources/10
POST /resources/
body of put containing a duplicate of the representation byGET /resources/10
without the id so that thePOST
creates a new resource.
这种方法的问题在于资源非常大且复杂,将完整的表示返回给客户端然后让客户端将其发送回去真的没有意义,因为这完全是在浪费带宽,并且服务器上的CPU.在服务器上克隆资源要容易得多,所以我想这样做.
The problem with this approach is that the resource is very large and complex it really makes no sense to return a full representation to the client and then have the client send it back as that would be just a total waste of bandwidth, and cpu on the server. Cloning the resource on the server is so much easier so I want to do that.
我可以做一些类似 POST/resources/10/clone
或 POST resources/clone/10
的事情,但这两种方法都感觉不对,因为 URL 中的动词.
I could do something like POST /resources/10/clone
or POST resources/clone/10
but both of these approaches feel wrong because the verb in the URL.
构建可用于此类情况的 url 的最宁静/无名"方式是什么?
What is the most "restful/nouny" way to build url that can be used in this type of situation?
推荐答案
由于 HTTP 中没有复制或克隆方法,因此您想要做什么完全取决于您.在这种情况下,POST
似乎完全合理,但其他标准采取了不同的方法:
Since there is no copy or clone method in HTTP, it's really up to you what you want to do. In this case a POST
seems perfectly reasonable, but other standards have taken different approaches:
- WebDAV 添加了
COPY
方法. - Amazon S3 使用
PUT
没有正文和特殊的x-amz-copy-source
标头.他们称之为PUT Object - Copy
一>.
- WebDAV added a
COPY
method. - Amazon S3 uses
PUT
with no body and a specialx-amz-copy-source
header. They call this aPUT Object - Copy
.
这两种方法都假设您知道目标 URI.您的示例似乎缺少已知的目标 uri,因此您几乎必须使用 POST.您不能使用 PUT 或 COPY,因为您的创建操作不是幂等的.
Both of these approaches assume that you know the destination URI. Your example seems to lack a known destination uri, so you pretty much must use POST. You can't use PUT or COPY because your creation operation is not idempotent.
如果您的服务将 POST/resources
定义为创建新资源",那么为什么不简单地定义另一种方式来指定资源而不是 POST 正文呢?例如.POST/resources?source=/resources/10
正文为空.
If your service defines POST /resources
as "create a new resource", then why not simply define another way to specify the resource other than as the body of the POST? E.g. POST /resources?source=/resources/10
with an empty body.
这篇关于在 URL 中表示资源克隆操作的宁静方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!