Wilson Canda
Wilson Canda

Reputation: 444

Using HTTP DELETE method to cancel an action in progress

Background:

I provide a programming interface for people who wish to place orders using my site.

They can use GET and POST methods on /api/v1/orders to place an order and to view list of all orders placed by them. They can also use the GET metod on /api/v1/orders/<order_id> in order to view specific details of one order.

There is a need to provide a way to cancel an order, however the records themselves need to be kept.

I would like to get feedback from more seasoned developers on whether it would be a sane decision to:

a) implement the DELETE verb on /api/v1/current_orders/<order_id>, which would delete it from the list of "current" orders (by marking it as cancelled). The downside is that it will make use of a different noun, which may be confusing.

b) implement the DELETE verb on /api/v1/orders/<order_id> with the same functionality as in a). This is somewhat misleading, as the entity will not really be deleted, and the consumer should be aware of that.

c) implement the POST verb on /api/v1/cancellations/<order_id> (or POST on /api/v1/cancellations with the order_id in the JSON payload). This seems to be less than ideal because a resource will not be created as a result of that request. However, the consequences of using this endpoint seem to be clearer.

d) ...?

Question:

I am aware that there is not always a "perfect" solution to designing endpoints for a REST API, but keeping in mind the need for clarity and intuitiveness and with a high regard for best practices, which of the options is "optimal"?

Upvotes: 8

Views: 3654

Answers (1)

vtortola
vtortola

Reputation: 35905

What about PATCH verb on /api/v1/orders/<order_id> indicating that it is cancelled?

HTTP PATCH : Allows to do partial modifications in an entity. While POST creates a new one, and PUT replaces an existing one, PATCH just updates the properties you are sending, leaves the rest at they are.

You would need only to send something like { isCancelled:true} as HTTP PATCH, then your code would update the entity and take action like cancel any outstanding work.

Upvotes: 8

Related Questions