Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to model a CANCEL action in a RESTful way?

We are currently in the process of wrangling smaller services from our monoliths. Our domain is very similar to a ticketing system. We have decided to start with the cancellation process of the domain.

Our cancel service has as simple endpoint "Cancel" which takes in the id of the ticket. Internally, we retrieve the id, perform some operations related to cancel on it and update the state of the entity in the store. From the store's perspective the only difference between a cancelled ticket and a live ticket are a few properties.

From what I have read, PATCH seems to be the correct verb to be used in this case, as am updating only a simple property in the resource.

PATCH /api/tickets/{id}
Payload {isCancelled: true}

But isCancelled is not an actual property in the entity. Is it fair to send properties in the payload that are not part of the entity or should I think of some other form of modeling this request? I would not want to send the entire entity as part of the payload, since it is large.

I have considered creating a new resource CancelledTickets, but in our domain we would never have the need to a GET on cancelled tickets. Hence stayed away from having to create a new resource.

like image 413
Karthik Balasubramanian Avatar asked Mar 01 '17 14:03

Karthik Balasubramanian


People also ask

How do I cancel REST API?

Firstly you need to use multiple threads because your program will be on hold while it is sending the request so you cannot click on something until it is back from hold. Create a thread which calls the rest API in background without hanging the overall application and terminate that thread on click of a button.

Can a HTTP request be Cancelled?

The Cancel Request API allows any data task or HTTP request to be cancelled.

Can we cancel API request?

If you are using REST, then it means you can never cancel the API call. If it gets submitted, it will continue processing in the backend even if you stop it in the frontend. But if 'Cancelling' is much important for your application through the API call, then you must consider other alternatives, like SOAP or RPC.

How do I interrupt http request?

We can use the AbortController object and the associated AbortSignal with the Fetch API to make cancelable HTTP requests. Once the AbortSignal is sent, the HTTP request is canceled and won't be sent if the cancellation signal is sent before the HTTP request is done.


2 Answers

Take a look what exactly is RESTful way. No matter if you send PATCH request with isCancelled as payload or even DELETE if you want tickets to disappear. It's still RESTful.

Your move depends on your needs. As you said

I have considered creating a new resource CancelledTickets, but in our domain we would never have the need to a GET on cancelled tickets.

I would just send DELETE. You don't have to remove it physically. If it's possible to un-cancel, then implement isCancelled mechanism. It's just question of taste.

like image 158
Piotr Dawidiuk Avatar answered Oct 20 '22 18:10

Piotr Dawidiuk


Exposing the GET interface of a resource is not compulsory.

For example, use

PUT /api/tickets/{id}/actions/cancel

to submit the cancellation request. I choose PUT since there would be no more than one cancellation request in effect.

Hope it be helpful.

like image 42
Israfel Avatar answered Oct 20 '22 18:10

Israfel