How to design REST API for non-CRUD “commands” like activate and deactivate of a resource?

前端 未结 4 729
隐瞒了意图╮
隐瞒了意图╮ 2020-12-14 01:01

Before I decided to ask this question I have searched quite a long for the answer but I haven\'t found any satisfactory. (e.g. Examples of the best SOAP/REST/RPC web APIs? A

4条回答
  •  暖寄归人
    2020-12-14 01:55

    The POST method would create the resource 'account'. Active can be seen as one of the properties of the resource 'account'. Hence it should be a PUT request.

    I would say even deactive must be a PUT request as the account resource will still exist.

    To active an account you can set a property on the resource. That is:

    /api/account/{accountId}?activate=true
    

    To deavtivate:

    /api/account/{accountId}?activate=false
    

    A GET request on the account would return a JSON with activate value in it.

    A DELETE request should completely delete the account resource.

提交回复
热议问题