Is it okay to use an HTTP DELETE to deactivate a record?

后端 未结 3 822
夕颜
夕颜 2021-02-19 05:48

I\'m building a RESTful API command to deactivate a user record. Is it kosher to use DELETE to do this or should this be a PUT, since the record is being updated to \"deactivate

3条回答
  •  执念已碎
    2021-02-19 06:25

    If the resource at the URL you send the DELETE request to is no longer available at that URI, then DELETE is appropriate. If it remains there but changes state, then it is not.

    e.g. this is okay (the resource at /friends/bob goes away; a new resource is created at /formerfriends/bob in the process, but that is incidental):

    GET /friends/bob => 200 OK
    GET /formerfriends/bob => 404 Not Found
    DELETE /friends/bob => 204 No Content
    GET /friends/bob => 410 Gone
    GET /formerfriends/bob => 200 OK
    

    this is not:

    GET /friends/bob => 200 OK {"status"="friend"}
    DELETE /friends/bob => 204 No Content
    GET /friends/bob => 200 OK {"status"="formerfriend"}
    

    something like that would be better handled with PUT or PATCH:

    GET /friends/bob => 200 OK {"status"="friend"}
    PATCH /friends/bob {"status"="formerfriend"} => 204 No Content
    GET /friends/bob => 200 OK {"status"="formerfriend"}
    

提交回复
热议问题