Gasim
Gasim

Reputation: 7961

Should PUT request be used to delete a resource?

I have a situation where I don't know how to manage it. I have a resource that has a flag "undeniable." Denying a post, would delete it from the database, so using

DELETE /v1/posts/post-id-here

would have been just fine; this way the undeniable post would have been just ignored with 404. But my undeniable post would have a counter on how many times it was getting denied, so I think this kills the purpose of DELETE. That's why I was heading towards PUT request but I can't find anywhere about validity of removing a resource on PUT request. I was thinking something like the following:

PUT /v1/posts/post-id-here/deny

which would either delete the post or increment the counter, with both returning 200 OK. Is it valid? Or should I try to separate them in the client side, where the undeniable posts output the PUT url and the deniable ones output the DELETE url; and if DELETE was called on undeniable one, 404 would be returned? Or is there a third option?

Upvotes: 0

Views: 409

Answers (1)

Thierry Templier
Thierry Templier

Reputation: 202176

The method PUT is used to update the complete state of the resource. I think that it's not exactly what you want to do. I would rather see the use of a method POST on the resource /v1/posts/post-id-here. This corresponds to the action of undenying the post. As far as I understand, such action will do the following:

  • Set the flag undeniable to true if its value was false before
  • Increment a counter (a field undenyCounter for example)

There is also the method PATCH that allows partial update of the resource state but I think that it's exactly your use case, since you want to increment a field...

To finish, using an action as a path parameter isn't really RESTful ;-) You can have a look at this question for more details about how to handle this: How to Update a REST Resource Collection.

Hope it helps you, Thierry

Upvotes: 1

Related Questions