Home  Puppeteer   How to capt ...

How to capture api response time and headers using puppeteer

To capture API request details, including response time, headers, and payload, using Puppeteer, you'll need to leverage the request interception mechanism and handle requests appropriately. Puppeteer itself doesn't directly expose API response times or headers, but you can calculate timings and access headers through intercepted requests. Here's a structured approach to achieve this:

  1. Enable Request Interception: Set up Puppeteer to intercept network requests.

  2. Capture Request Details: Extract relevant information from intercepted requests.

  3. Calculate Response Time: Compute the response time based on request initiation and response completion.

Here's how you can implement this:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  // Enable request interception
  await page.setRequestInterception(true);

  // Array to store API request details
  const apiRequests = [];

  // Event listener for request interception
  page.on('request', interceptedRequest => {
    // Filter requests based on URL or other criteria
    if (interceptedRequest.url().includes('/api/')) {
      const requestStartTime = new Date();

      // Continue intercepting the request
      interceptedRequest.continue().then(() => {
        // Event listener for response interception
        interceptedRequest.response().then(response => {
          const requestEndTime = new Date();
          const requestDuration = requestEndTime - requestStartTime;

          // Extract response headers and payload
          const headers = response.headers();
          response.text().then(body => {
            apiRequests.push({
              url: interceptedRequest.url(),
              method: interceptedRequest.method(),
              status: response.status(),
              headers: headers,
              payload: body,
              responseTime: requestDuration,
            });
          });
        });
      });
    } else {
      // Continue all other requests normally
      interceptedRequest.continue();
    }
  });

  // Navigate to a website that triggers API requests
  await page.goto('https://example.com');

  // Wait for a few seconds to capture network requests
  await page.waitForTimeout(5000); // Adjust as needed

  // Display captured API requests
  console.log('Captured API Requests:', apiRequests);

  await browser.close();
})();

Explanation:

Published on: Jun 28, 2024, 12:23 AM  
 

Comments

Add your comment