Harkal
Harkal

Reputation: 1828

how to intercept all http requests in angular

i m creating a WPA using angular 8

yesterday i read about service workers and also implemented them in plain javascript. they worked well and gave me full access over all network requests

but today when i implemented service workes in angular 8 i got to know they can only intercept http calls made by httclient [not so helpful]

what i want is even if i try to do a get request using jquery directly from console i should get a note

and yesterday i could do it with plain js implementation

this doubt is not realed to coding at all

all i want to know is : is there a way to get low level access to the fetch requests of service worker ? or will i have give up the idea of httpInterceptor and implement my own logic using plain js ?

please help any kind of arguments are welcomed

Upvotes: 2

Views: 5926

Answers (3)

Ishaan Kumar
Ishaan Kumar

Reputation: 985

Sample code for intercepting calls made to Fetch API using Proxy.

window.fetch = new Proxy(window.fetch, {
    apply(target, thisArg, argsList) {
        //do your stuff;
        return target;
    },
});

Sample code for intercepting calls made through XMLHttpRequest using prototype.

(function(send) { 
    XMLHttpRequest.prototype.send = function(data) { 
        //do your stuff;
        send.call(this, data);

    }; 
})(XMLHttpRequest.prototype.send);

Credits: https://stackoverflow.com/a/23539487

Upvotes: 2

Chris Love
Chris Love

Reputation: 3893

You can't intercept HTTP request in the UI. All HTTP requests pass through an active service worker. That is where you intercept the request, not in the UI.

Upvotes: 0

shaikh
shaikh

Reputation: 19

I think The angular HttpInterceptor will only intercept requests that you make using HttpClient only provided by angular. It will not intercept any requests made in plain .js or jQuery

Upvotes: 1

Related Questions