You've to use a proxy like BrowserMob Proxy or MITMProxy.
I've started an extension for Codeception: https://github.com/edno/codeception-browsermob
I had a look at the mitmproxy as well. I wanted to build a docker image that contains a headless chrome, webdriver and the proxy.
I found a solutions that works for us (www.leankoala.com). As we are only using chrome it was possible to write a chrome extension that injects the needed code.
You can also use extension like Tampermonkey, it is available for Firefox and Chrome. The issue with extension is when you have to test outside those browsers, i.e. Internet Explorer, Safari, or mobile browsers.