Should I use PATCH or PUT in my REST API?

前端 未结 6 1085
予麋鹿
予麋鹿 2020-11-30 16:33

I want to design my rest endpoint with the appropriate method for the following scenario.

There is a group. Each group has a status. The group can be activated or in

6条回答
  •  遥遥无期
    2020-11-30 16:38

    One possible option to implement such behavior is

    PUT /groups/api/v1/groups/{group id}/status
    {
        "Status":"Activated"
    }
    

    And obviously, if someone need to deactivate it, PUT will have Deactivated status in JSON.

    In case of necessity of mass activation/deactivation, PATCH can step into the game (not for exact group, but for groups resource:

    PATCH /groups/api/v1/groups
    {
        { “op”: “replace”, “path”: “/group1/status”, “value”: “Activated” },
        { “op”: “replace”, “path”: “/group7/status”, “value”: “Activated” },
        { “op”: “replace”, “path”: “/group9/status”, “value”: “Deactivated” }
    }
    

    In general this is idea as @Andrew Dobrowolski suggesting, but with slight changes in exact realization.

提交回复
热议问题