How Can I Edit the Body of an API Request With a Firefox Addon

I’m trying to write a firefox addon that modifies the body of a background http request to an api. I found webRequest.filterResponseData(), but it did not work for my use case:

If I go directly to the api url, the response body gets changed by the filter.

If I go to the website that makes the request in the background as it loads, the filter doesn’t change the response body. I debugged the addon and saw that the response was being sent to the loading page and only after that filter.ondata was called, so the filter had no effect.

Does anyone know how can I make sure the filter is applied before the api response is passed to the page being loaded?