Implementation:Puppeteer Puppeteer Bidi HTTPRequest
| Property | Value |
|---|---|
| Implementation | BidiHTTPRequest |
| Source File | packages/puppeteer-core/src/bidi/HTTPRequest.ts
|
| Repository | puppeteer/puppeteer |
| Lines | 354 |
| License | Apache-2.0 |
| Copyright | 2020 Google Inc. |
Overview
Description
BidiHTTPRequest is the WebDriver BiDi implementation of the Puppeteer HTTPRequest abstract class. It wraps a low-level BiDi Request object and provides the high-level interface for inspecting and intercepting HTTP requests made by the browser.
Key responsibilities include:
- Request inspection: Provides accessors for URL, method, headers, post data, resource type, initiator, and navigation status.
- Response tracking: Maintains a reference to the associated
BidiHTTPResponsethat is created whennetwork.responseStartedandnetwork.responseCompletedevents are received. - Request interception: Supports continuing, aborting, and responding to intercepted requests via
_continue(),_abort(), and_respond(). - Redirect chain: Tracks the chain of redirects by listening for redirect events on the underlying request and creating new
BidiHTTPRequestinstances for each redirect. - Authentication handling: Automatically provides credentials or cancels authentication prompts based on page-level credential settings.
- Timing and content: Exposes
timing()for fetch timing info andgetResponseContent()for retrieving the response body.
The module also exports a requests WeakMap that maps low-level Request objects to their corresponding BidiHTTPRequest, enabling cross-referencing from navigation code.
Usage
BidiHTTPRequest instances are not created directly by users. They are created internally by BidiFrame when a network request event is received from the underlying browsing context. Users interact with these objects through page.on('request'), page.on('requestfinished'), and page.on('requestfailed') event handlers, as well as through the request interception API.
Code Reference
Source Location
packages/puppeteer-core/src/bidi/HTTPRequest.ts (GitHub)
Signature
export const requests = new WeakMap<Request, BidiHTTPRequest>();
export class BidiHTTPRequest extends HTTPRequest {
static from(
bidiRequest: Request,
frame: BidiFrame,
isNetworkInterceptionEnabled: boolean,
redirect?: BidiHTTPRequest,
): BidiHTTPRequest;
override readonly id: string;
override get client(): CDPSession;
override url(): string;
override resourceType(): ResourceType;
override method(): string;
override postData(): string | undefined;
override hasPostData(): boolean;
override async fetchPostData(): Promise<string | undefined>;
override headers(): Record<string, string>;
override response(): BidiHTTPResponse | null;
override failure(): {errorText: string} | null;
override isNavigationRequest(): boolean;
override initiator(): Protocol.Network.Initiator | undefined;
override redirectChain(): BidiHTTPRequest[];
override frame(): BidiFrame;
override async _continue(overrides?: ContinueRequestOverrides): Promise<void>;
override async _abort(): Promise<void>;
override async _respond(response: Partial<ResponseForRequest>, _priority?: number): Promise<void>;
timing(): Bidi.Network.FetchTimingInfo;
getResponseContent(): Promise<Uint8Array>;
}
Import
import {BidiHTTPRequest, requests} from './HTTPRequest.js';
I/O Contract
Inputs
| Parameter | Type | Description |
|---|---|---|
| bidiRequest | Request |
The low-level BiDi core Request object |
| frame | BidiFrame |
The frame that initiated the request |
| isNetworkInterceptionEnabled | boolean |
Whether network interception is currently enabled |
| redirect | BidiHTTPRequest |
Optional previous request if this is a redirect |
Key Method Parameters
| Method | Parameter | Type | Description |
|---|---|---|---|
| _continue | overrides | ContinueRequestOverrides |
Optional overrides for url, method, postData, headers |
| _respond | response | Partial<ResponseForRequest> |
Response data including status, headers, contentType, body |
Outputs
| Method | Return Type | Description |
|---|---|---|
| url | string |
The URL of the request |
| method | string |
The HTTP method (GET, POST, etc.) |
| headers | Record<string, string> |
The request headers as key-value pairs |
| response | null | The associated response, or null if not yet received |
| failure | null | Error information if the request failed, or null |
| redirectChain | BidiHTTPRequest[] |
Array of requests in the redirect chain |
| isNavigationRequest | boolean |
Whether this request was triggered by navigation |
| timing | Bidi.Network.FetchTimingInfo |
Fetch timing information for the request |
| getResponseContent | Promise<Uint8Array> |
The response body content |
Usage Examples
// Listen for requests
page.on('request', (request) => {
console.log(request.url());
console.log(request.method());
console.log(request.headers());
});
// Intercept and modify requests
await page.setRequestInterception(true);
page.on('request', async (request) => {
if (request.url().includes('api.example.com')) {
await request.continue({
url: request.url().replace('api.example.com', 'mock.example.com'),
headers: {
...request.headers(),
'X-Custom-Header': 'test',
},
});
} else {
await request.continue();
}
});
// Abort specific requests
page.on('request', async (request) => {
if (request.resourceType() === 'image') {
await request.abort();
} else {
await request.continue();
}
});
// Provide a custom response
page.on('request', async (request) => {
if (request.url().endsWith('/data.json')) {
await request.respond({
status: 200,
contentType: 'application/json',
body: JSON.stringify({mock: true}),
});
} else {
await request.continue();
}
});
// Inspect redirect chain
page.on('requestfinished', (request) => {
const chain = request.redirectChain();
if (chain.length > 0) {
console.log(`Request redirected ${chain.length} times`);
}
});