What is the restful way to represent a resource clone operation in the URL?

后端 未结 5 2149
执笔经年
执笔经年 2020-12-07 23:02

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}

5条回答
  •  青春惊慌失措
    2020-12-07 23:12

    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 added a COPY method.
    • Amazon S3 uses 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.

提交回复
热议问题