Implementation:Puppeteer Puppeteer Page SetRequestInterception
| Knowledge Sources | |
|---|---|
| Domains | Browser_Automation, Networking |
| Last Updated | 2026-02-11 23:00 GMT |
Overview
Concrete tool for enabling network request interception on a Puppeteer Page.
Description
Page.setRequestInterception() enables or disables HTTP request interception for a page. When enabled, all network requests are paused and emitted as request events on the page. The handler must call continue(), abort(), or respond() on each intercepted request.
In the CDP backend, this is implemented via the Fetch.enable and Network.setRequestInterception CDP commands, managed by NetworkManager.
Usage
Call this method before navigation to intercept all requests. Register a handler via page.on('request', handler) to process intercepted requests.
Code Reference
Source Location
- Repository: puppeteer
- setRequestInterception: packages/puppeteer-core/src/api/Page.ts (line 1002, abstract)
- Page.on (request handler wrapping): packages/puppeteer-core/src/api/Page.ts (lines 800-825)
- NetworkManager (CDP): packages/puppeteer-core/src/cdp/NetworkManager.ts (lines 1-834)
Signature
abstract class Page {
abstract setRequestInterception(value: boolean): Promise<void>;
// Event registration for request handling
on(event: 'request', handler: (request: HTTPRequest) => void): this;
}
Import
const page = await browser.newPage();
await page.setRequestInterception(true);
page.on('request', request => {
// handle request
});
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| value | boolean | Yes | true to enable interception, false to disable |
Outputs
| Name | Type | Description |
|---|---|---|
| return | Promise<void> | Resolves when interception mode has been set |
| events | 'request' events | Each intercepted request emits a 'request' event with an HTTPRequest object |
Usage Examples
Block Images
const puppeteer = require('puppeteer');
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setRequestInterception(true);
page.on('request', request => {
if (request.resourceType() === 'image') {
request.abort();
} else {
request.continue();
}
});
await page.goto('https://example.com');
await browser.close();
Mock API Response
await page.setRequestInterception(true);
page.on('request', request => {
if (request.url().includes('/api/data')) {
request.respond({
status: 200,
contentType: 'application/json',
body: JSON.stringify({mock: true, items: [1, 2, 3]}),
});
} else {
request.continue();
}
});
Modify Request Headers
await page.setRequestInterception(true);
page.on('request', request => {
request.continue({
headers: {
...request.headers(),
'Authorization': 'Bearer my-token',
},
});
});