Principle:Puppeteer Puppeteer Request Handling
| Knowledge Sources | |
|---|---|
| Domains | Browser_Automation, Networking, Testing |
| Last Updated | 2026-02-11 23:00 GMT |
Overview
A set of operations that resolve intercepted HTTP requests by continuing, aborting, or responding with custom data.
Description
Request Handling provides three resolution strategies for intercepted HTTP requests:
- Continue: Forward the request to the network. Optionally modify the URL, method, headers, or POST data before forwarding.
- Abort: Cancel the request and return an error to the page. Useful for blocking unwanted resources.
- Respond: Fulfill the request with a custom response without touching the network. Useful for mocking APIs and injecting test data.
Puppeteer supports cooperative intercept handling: multiple handlers can be registered for request events, and a priority-based resolution system determines the final action. This enables layering interceptors (e.g., one for authentication headers, another for ad blocking).
Usage
Use request handling within request interception handlers (registered via page.on('request', ...)). Every intercepted request must be resolved with exactly one of these three methods. Failing to call any of them will cause the request to hang indefinitely.
Theoretical Basis
# Request resolution decision tree
intercepted_request:
if should_block(request):
request.abort('blockedbyclient')
else if should_mock(request):
request.respond({status, headers, body})
else if should_modify(request):
request.continue({url, method, headers, postData})
else:
request.continue() // pass through unchanged