Update an entire resource collection in a REST way

后端 未结 3 1332
礼貌的吻别
礼貌的吻别 2020-11-28 10:14

I have a REST URI for a list of resources, something like:

http://foo.com/group/users

Each of these users has a sequence number and I want

3条回答
  •  甜味超标
    2020-11-28 10:43

    After the raffian's comment on my initial response, I reworked my answer to be more RESTful...

    • Use the method PATCH

    This method is typically designed to update partially the state of a resource. In the case of a list resource, we could send a list with only the elements to update and the identifiers of elements in the list. The following request would be:

    PATCH /group/users
    [
        { "id": "userId1", "sequence": "newSequenceNumber1" },
        { "id": "userId2", "sequence": "newSequenceNumber2" },
        (...)
    ]
    
    • Use the method POST on the list resource

    This method is commonly used to add an element in the list managed by the resource. So if you want to leverage it for this action, we need to pass within the request an hint regarding the action to execute. We have the choice to add this either in a dedicated header or within the payload.

    With the header approach, you will have something like that:

    POST /group/users
    X-Action: renumbering
    [
        { "id": "userId1", "sequence": "newSequenceNumber1" },
        { "id": "userId2", "sequence": "newSequenceNumber2" },
        (...)
    ]
    

    With the payload approach, you will have something like that:

    POST /group/users
    {
        "action": "renumbering",
        "list": {
            [
                { "id": "userId1", "sequence": "newSequenceNumber1" },
                { "id": "userId2", "sequence": "newSequenceNumber2" },
                (...)
            ]
        }
    }
    

    Hope it helps you, Thierry

提交回复
热议问题