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}
To clone resource 10 I could do something like.
GET /resources/10
POST /resources/
body of put containing a duplicate of the representation by GET /resources/10
without the id so that the POST
creates a new resource.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.
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.
What is the most "restful/nouny" way to build url that can be used in this type of situation?
Although URLs containing parameters within the query string do themselves conform to REST constraints, the term “REST-style URL” is often used to signify a URL that contains its parameters within the URL file path, rather than the query string.
REST uses various representations to represent a resource where Text, JSON, XML. The most popular representations of resources are XML and JSON.
URL Structure The recommended convention for URLs is to use alternate collection / resource path segments, relative to the API entry point.
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:
COPY
method.PUT
with no body and a special x-amz-copy-source
header. They call this a PUT Object - 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With