0

I am trying to intercept the HTTP call, made using the fetch API. I read through:

and was trying the same thing, but I am not able to intercept the request made from the fetch function.

Here is what I am doing:

(function(open) {
    XMLHttpRequest.prototype.open = function() {
        this.addEventListener("readystatechange", function() {
            console.log(this.readyState);
        }, false);
        open.apply(this, arguments);
    };
})(XMLHttpRequest.prototype.open);


fetch("http://jsonplaceholder.typicode.com/posts")
    .then(response => {
        console.log(response);
    })
    .catch(err => {
        console.log(err);
    })

I receive the response without interceptor being invoked. What is it that I am missing? I want to add an intercept to HTTP request no matter if the application were created using ReactJS, Angular or vanilla JS.

Amanda
  • 2,013
  • 3
  • 24
  • 57

1 Answers1

0

I think you should use this to intercept the fetch calls

        const constantMock = window.fetch;
 window.fetch = function() {
     // Get the parameter in arguments
     // Intercept the parameter here
console.log(arguments);
    return constantMock.apply(this, arguments)
 }

Found in this post link