How can I intercept and modify network requests in Puppeteer?
Ava W
ava w profile pic

In Puppeteer, a Node.js library for controlling headless Chrome or Chromium browsers, you can intercept and modify network requests using thepage.setRequestInterception() method along with therequest event. Here is a long-form answer that explains the process step-by-step: 1. Enable request interception:

1
2
3

   await page.setRequestInterception(true);
   

This code enables request interception on the page by setting thetrue parameter forsetRequestInterception(). Once enabled, Puppeteer will intercept all network requests made by the page. 2. Register therequest event listener:

1
2
3
4
5

   page.on('request', (interceptedRequest) => {
     // Handle intercepted requests here
   });
   

This code registers an event listener for therequest event, which is emitted whenever a network request is intercepted. Inside the event listener, you can perform actions on the intercepted request. 3. Handle intercepted requests:

1
2
3
4
5
6
7
8
9
10

   page.on('request', (interceptedRequest) => {
     // Example: Modify the request URL
     if (interceptedRequest.url().includes('example.com')) {
       interceptedRequest.continue({ url: 'https://modified-example.com' });
     } else {
       interceptedRequest.continue();
     }
   });
   

In the event listener, you can inspect and modify intercepted requests. In this example, the code checks if the request URL includes'example.com'. If it does, thecontinue() method is called on the intercepted request with a modified URL provided as an object parameter. If the URL doesn't match the condition, the request is continued without modification usinginterceptedRequest.continue(). You can also modify other aspects of the request, such as headers or request method, by providing the respective properties in the object passed tocontinue(). 4. Disable request interception when done: Once you have finished intercepting requests, you can disable request interception to allow the page to continue normal network requests:

1
2
3

   await page.setRequestInterception(false);
   

This code setssetRequestInterception(false) to disable request interception. By following these steps, you can intercept and modify network requests in Puppeteer. This capability allows you to manipulate requests for various purposes, such as modifying URLs, injecting headers, or blocking specific requests. Remember to handle the intercepted requests appropriately based on your specific use case and modify them using the available methods provided by Puppeteer.