Overview
Concrete tools for resolving intercepted HTTP requests via continue, abort, or custom response, provided by the puppeteer-core library.
Description
The HTTPRequest class provides three resolution methods for intercepted requests. The public methods delegate to internal abstract methods (_continue, _abort, _respond) that are implemented differently in CDP and BiDi backends. The public wrappers handle cooperative intercept logic via enqueueInterceptAction.
Usage
Call exactly one of these methods on each intercepted HTTPRequest object received in a page.on('request', ...) handler.
Code Reference
Source Location
- Repository: puppeteer
- File: packages/puppeteer-core/src/api/HTTPRequest.ts
- Abstract methods: lines 239-251
- Full class: lines 1-750
- CDP impl: packages/puppeteer-core/src/cdp/HTTPRequest.ts (lines 1-301)
- BiDi impl: packages/puppeteer-core/src/bidi/HTTPRequest.ts (lines 1-354)
Signature
abstract class HTTPRequest {
// Continue the request, optionally with modifications
async continue(
overrides?: ContinueRequestOverrides,
priority?: number,
): Promise<void>;
// Abort the request
async abort(
errorCode?: ErrorCode,
priority?: number,
): Promise<void>;
// Respond with a custom response
async respond(
response: Partial<ResponseForRequest>,
priority?: number,
): Promise<void>;
// Inspection methods
url(): string;
method(): string;
resourceType(): ResourceType;
headers(): Record<string, string>;
postData(): string | undefined;
response(): HTTPResponse | null;
}
Import
// HTTPRequest objects are received in event handlers
page.on('request', (request) => {
request.continue(); // or abort() or respond()
});
I/O Contract
Inputs (continue)
| Name |
Type |
Required |
Description
|
| overrides.url |
string |
No |
Modified URL
|
| overrides.method |
string |
No |
Modified HTTP method
|
| overrides.headers |
Record<string, string> |
No |
Modified headers
|
| overrides.postData |
string |
No |
Modified POST body
|
| priority |
number |
No |
Priority for cooperative handling (higher wins)
|
Inputs (abort)
| Name |
Type |
Required |
Description
|
| errorCode |
ErrorCode |
No |
Error reason: 'aborted', 'failed', 'blockedbyclient', etc. (default: 'failed')
|
| priority |
number |
No |
Priority for cooperative handling
|
Inputs (respond)
| Name |
Type |
Required |
Description
|
| response.status |
number |
No |
HTTP status code (default: 200)
|
| response.headers |
Record<string, string> |
No |
Response headers
|
| response.contentType |
string |
No |
Content-Type header shorthand
|
| response.body |
string or Uint8Array |
No |
Response body
|
| priority |
number |
No |
Priority for cooperative handling
|
Outputs
| Name |
Type |
Description
|
| return |
Promise<void> |
Resolves when the resolution has been applied
|
Usage Examples
Block Images (From Puppeteer Examples)
const puppeteer = require('puppeteer');
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setRequestInterception(true);
page.on('request', request => {
if (['image', 'stylesheet', 'font'].includes(request.resourceType())) {
request.abort();
} else {
request.continue();
}
});
await page.goto('https://news.google.com/news/');
await page.screenshot({path: 'no-images.png', fullPage: true});
await browser.close();
Mock JSON API
page.on('request', request => {
if (request.url().endsWith('/api/users')) {
request.respond({
status: 200,
contentType: 'application/json',
body: JSON.stringify([{id: 1, name: 'Test User'}]),
});
} else {
request.continue();
}
});
Related Pages
Implements Principle
Requires Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.