1

I'm trying to intercept POST request made by the website and intercept the response but somehow I am not getting the correct result.

I'm trying this same on Facebook ads library and trying to fetch the response body, I can see the POST request made by checking the "Network tab" in the browser. I want to fetch those responses within the code and then save it for later use.

chrome.webRequest.onBeforeRequest.addListener(
  function (details) {
    if (details.method == "POST")
      // Use this to decode the body of your post
      var postedString = decodeURIComponent(
        String.fromCharCode.apply(
          null,
          new Uint8Array(details.requestBody.raw[0].bytes)
        )
      );
    console.log(postedString);
  },
  { urls: ["<all_urls>"] },
  ["requestBody"]
);

That's what I have written/checked from stackoverflow so far.

Cosmin Staicu
  • 1,809
  • 2
  • 20
  • 27
Frosty
  • 48
  • 5
  • "I am not getting the correct result." --- What is the expected result, and what is the actual result? – Thomas Mueller Dec 14 '22 at 16:13
  • Actual result is the payload of data in facebook ads library webpage like each ad detail but what I am getting is "undefined", It's because I am not fetching any POST method, if I change the if condition to "GET" I will get tons of request like 300 but no POST request, even in networking tab I see post request being made. For Get method, I am also not able to parse the body or see the response. – Frosty Dec 14 '22 at 16:26
  • Your code intercepts request body not response body. Note that GET requests don't have any request body. Again, request body is not response body. See [Chrome extension to read HTTP response](https://stackoverflow.com/q/8939467) + [Chrome Extension - How to get HTTP Response Body?](https://stackoverflow.com/q/18534771) + [Modify HTTP responses from a Chrome extension](https://stackoverflow.com/q/18310484). – wOxxOm Dec 14 '22 at 16:56
  • @wOxxOm Those posts are outdated and works for manifest version 2, is it possible to have anything for manifest version 3? – Frosty Dec 14 '22 at 18:26
  • The approach is still the same. – wOxxOm Dec 14 '22 at 18:55

0 Answers0