Minimal puppeteer response interception example
Using Python (pyppeteer)? Check out Pyppeteer minimal network response interception example
This example shows you how to intercept network responses in puppeteer. Note: This intercepts the response, not the request! This means you can’t abort the request before it is actually sent to the server, but you can read the content of the response! See Minimal puppeteer request interception example for an example on how to intercept requests.
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Enable response interception
page.on('response', async (response) => {
console.info("URL", response.request().url());
console.info("Method", response.request().method())
console.info("Response headers", response.headers())
console.info("Request headers", response.request().headers())
// Use this to get the content as text
const responseText = await response.text();
// ... or as buffer (for binary data)
const responseBuffer = await response.buffer();
// ... or as JSON, if it's a JSON (else, this will throw!)
const responseObj = await response.json();
})
await page.goto('https://techoverflow.net', {waitUntil: 'domcontentloaded'});
// Make a screenshot
await page.screenshot({path: 'screenshot.png'});
await browser.close();
})();