+1

what function are included in CdpHTTPResponse

The HTTPResponse class provides various functions to access information about the HTTP response. Here are some key functions included in the HTTPResponse class:

HTTPResponse Class Functions url(): Returns the URL of the response. status(): Returns the status code of the response (e.g., 200, 404). statusText(): Returns the status text corresponding to the status code (e.g., OK, Not Found). ok(): Returns true if the status code is in the range 200-299. headers(): Returns an object containing all response headers. securityDetails(): Returns an object with security details (e.g., SSL certificate information) if available. buffer(): Returns a Promise that resolves to the response body as a Buffer. text(): Returns a Promise that resolves to the response body as a string. json(): Returns a Promise that resolves to the response body parsed as JSON. request(): Returns the HTTPRequest object that was sent to get this response. fromCache(): Returns true if the response was served from the browser's cache. fromServiceWorker(): Returns true if the response was served by a service worker. Example Usage Here's an example demonstrating how to capture and log HTTP responses using Puppeteer:

const puppeteer = require('puppeteer');
(async () => {
  const browser = await puppeteer.launch({ headless: false });
  const page = await browser.newPage();

  // Intercept and log HTTP responses
  page.on('response', async (response) => {
    const url = response.url();
    const status = response.status();
    const statusText = response.statusText();
    const headers = response.headers();
    const responseBody = await response.text();

    console.log(`Response URL: ${url}`);
    console.log(`Status: ${status} ${statusText}`);
    console.log(`Headers:`, headers);
    console.log(`Response Body:`, responseBody);
  });

  // Navigate to your target page
  await page.goto('https://example.com', { waitUntil: 'networkidle2' });

  // Continue with your Puppeteer interactions
  // Example: Clicking a button
  // await page.click('button-selector');

  // Example waiting for some time to capture HTTP responses
  await page.waitForTimeout(10000); // Adjust time as needed

  // Close browser when done
  await browser.close();
})();

Explanation: Interception of Responses: The page.on('response', ...) event listener intercepts all HTTP responses. Logging Details: For each response, the script logs the URL, status, status text, headers, and response body. Utility Functions: The script uses the url(), status(), statusText(), headers(), and text() functions of the HTTPResponse class to retrieve and log these details. Note: Make sure to handle large response bodies carefully to avoid excessive memory usage. Use appropriate error handling and logging for production use. By using the functions provided by the HTTPResponse class, you can effectively capture and process HTTP responses in Puppeteer.


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí