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

后端 未结 5 2151
执笔经年
执笔经年 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:15

    I think POST /resources/{id} would be a good solution to copy a resource.

    Why?

    • POST /resources is the default REST-Standard to CREATE a new resource
    • POST /resources/{id} should not be possible in most REST apis, because that id already exists - you will never generate a new resource with you (the client) defining the id. The server will define the id.

    Also note that you will never copy resource A on resource B. So if you want to copy existing resource with id=10, some answers suggest this kind of thing:

    POST /resources?sourceId=10
    
    POST /resources?copyid=10
    

    but this is simpler:

    POST /resources/10
    

    which creates a copy of 10 - you have to retrieve 10 from storage, so if you don't find it, you cannot copy it = throw a 404 Not Found.

    If it does exist, you create a copy of it.

    So using this idea, you can see it does not make sense to do the following, copying some b resource to some a resource:

    POST /resources?source=/resources/10
    POST /resources-a?source=/resources-b/10
    

    So why not simply use POST /resources/{id}

    • It will CREATE a new resource
    • The copy parent is defined by the {id}
    • The copy will be only on the same resource
    • It's the most REST-like variant

    What do you think about this?

提交回复
热议问题