Skip to content

Instantly share code, notes, and snippets.

@d-kja
Last active June 6, 2024 20:53
Show Gist options
  • Save d-kja/a5bbe2ed9b368de2be2a3d5f4bb70d2b to your computer and use it in GitHub Desktop.
Save d-kja/a5bbe2ed9b368de2be2a3d5f4bb70d2b to your computer and use it in GitHub Desktop.
intercept-response-body-puppeteer
const client = await page.target().createCDPSession();
await client.send("Fetch.enable", {
patterns: [{ requestStage: "Response" }]
});
client.on('Fetch.requestPaused', async parameters => {
const { requestId } = parameters
const responseCdp = await client.send("Fetch.getResponseBody", { requestId });
const responseBody = responseCdp.base64Encoded ? Buffer.from(responseCdp.body, 'base64') : responseCdp.body;
console.log({
bodyLength: responseCdp.body.length,
body: responseBody.toString()
});
await client.send("Fetch.continueRequest", { requestId });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment