0

I need to cancel a httpRequest with DELETE verb before it finishes (because user has canceled the operation)

The solution I have found seems to be just canceling the subscription to the observable, but if the httpRequest was sent and process by the API is not really canceling the request, is just canceling the reaction to the request finishing - https://stackoverflow.com/a/36491025/1461862

Is there a way to cancel the actual request (not the subscription) before if finishes ?

export class MileageTrackingService extends BaseService {
    public deleteByDomainId(id, domainId: string) {
        const params = new HttpParams().set('domainId', domainId)

        return this.httpClient.delete<any>(`API-URL/mil-track/${id}`, { params });
    }
}

E_net4
  • 27,810
  • 13
  • 101
  • 139
Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
  • Not that I know of. See here for reference: https://softwareengineering.stackexchange.com/questions/362187/can-a-caller-abort-an-execution-of-code-invoked-by-http-request – Matt U Jan 06 '22 at 02:27
  • I believe you need [this](https://stackoverflow.com/a/17328336/12914274). – Yevgeniy Kosmak Jan 06 '22 at 02:29
  • 1
    @YevgeniyKosmak The comment on the question you linked is very relevant: "None of the answers below actually cancel the request itself. There is no way to cancel a HTTP request once it leaves the browser." – Amadan Jan 06 '22 at 02:30
  • @Amadan is a relevant comment that Is not answered on that thread – Mauricio Gracia Gutierrez Jan 06 '22 at 02:31
  • The best you can do is save enough information on the backend for an undo operation, and send another request to trigger an undo. Your request is like sending a spouse to buy a jacket, but they don't have a mobile phone. The only way to "cancel" it is to wait till they come back, then send them out again to ask the store for a refund. – Amadan Jan 06 '22 at 02:34
  • 1
    @YevgeniyKosmak - thanks for the info I guess building and sending the request happens so fast that it was not designed to be canceled, just timed out. – Mauricio Gracia Gutierrez Jan 06 '22 at 02:35
  • 1
    There is no answer for that comment because that is a mic drop; no answer is possible. Again, there is _no way_ to cancel a HTTP request once it leaves the browser. If the task on the server is taking some time, you can have a record in a database for it with a field like `aborted`, meanwhile, you can send another request to set the `abort` flag on the job. The process that executes your task can then periodically check that database flag to see if it should abort execution. – Amadan Jan 06 '22 at 02:38
  • Thanks for your comment @Amadan - you keep saying "browser" but there is none when is an httpClient making http requests - also if you put your comment as answer I can give you credit for it. – Mauricio Gracia Gutierrez Jan 06 '22 at 03:10

0 Answers0