How to cancel an httpclient delete request before it finishes
Asked Answered
G

0

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://mcmap.net/q/234475/-how-to-cancel-a-httprequest-in-angular-2

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 });
    }
}

Greatniece answered 6/1, 2022 at 2:24 Comment(8)
Not that I know of. See here for reference: softwareengineering.stackexchange.com/questions/362187/…Listed
I believe you need this.Yoo
@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."Fletcher
@Fletcher is a relevant comment that Is not answered on that threadGreatniece
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.Fletcher
@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.Greatniece
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.Fletcher
Thanks for your comment @Fletcher - 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.Greatniece

© 2022 - 2024 — McMap. All rights reserved.