Does Puppeteer Follow Redirects Automatically When Request Interception Is Enabled?
0 reputation · 02 Aug 2022, 20:17 UTC
0 reputation · 02 Aug 2022, 20:17 UTC
When request interception is enabled in Puppeteer via page.setRequestInterception(true), each outgoing request is paused and must be resolved with request.continue(), request.abort(), or request.override(). The goal is to determine whether Puppeteer automatically follows HTTP redirects (e.g., 301/302 responses) under interception, or if developers must manually inspect the Location header and issue a new request to emulate redirect behavior. This uncertainty affects test reliability when mocking APIs or simulating network conditions.
Does Puppeteer follow redirects automatically when interception is active? Should developers manually read the Location header and issue a new request to reproduce the redirect? Is there a way to enable automatic redirect handling while keeping interception enabled?
When page.setRequestInterception(true) is active, Puppeteer does not transparently follow HTTP redirects. Each 3xx response generates a new request event for the Location URL, and the original request receives the redirect response. You must explicitly allow or abort each hop.
request event listener.request.continue() to let the request proceed.request event is fired for the redirect target URL.request handler, call request.continue() for every request, including those that are part of a redirect chain.request.abort() on the target redirect request.Location header, read request.headers() or the response’s response.headers() after request.continue() resolves.The interception API deliberately exposes every network request, including those generated by redirects. There is no flag to hide redirects while keeping interception enabled.
await page.setRequestInterception(false);
await page.goto('https://example.com');
await page.setRequestInterception(true);
This lets Chrome follow redirects automatically, then you re‑enable interception for subsequent sub‑resource requests.const client = await page.target().createCDPSession();
await client.send('Network.setRequestInterception', {
patterns: [{ urlPattern: '*', requestStage: 'Response' }]
});
But this still gives you a requestIntercepted event for each redirect.page.on('request', request => {
const url = request.url();
console.log('Request:', url);
// Let every request proceed – redirects will trigger new events
request.continue();
});
Do you need to preserve the original navigation state (e.g., keep the page at the final URL) while mocking only API calls, or do you want to mock every request including redirects? This determines whether the “disable‑then‑enable” pattern is appropriate.
request.url() in the event handler.Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.