Rayrun

How do I retrieve the value of the 'Date' header from a request using Playwright's request.headersArray() method?

Answer

To get the 'Date' header value using request.headersArray() in @playwright/test, follow these steps:

  1. Call request.headersArray() to get an array of header objects:
const headers = await request.headersArray();
  1. Loop through the array and find the 'Date' header:
let dateHeader;
for (const obj of headers) {
  if (obj.name.toLowerCase() === 'date') {
    dateHeader = obj.value;
    break;
  }
}

Now, dateHeader contains the value of the 'Date' header. You can use this value as needed in your code.

Here's a complete example using @playwright/test:

import { test, expect } from '@playwright/test';

test('Get Date header value', async ({ page }) => {
  await page.route('https://ray.run/**', async (route, request) => {
    const headers = await request.headersArray();
    let dateHeader;
    for (const obj of headers) {
      if (obj.name.toLowerCase() === 'date') {
        dateHeader = obj.value;
        break;
      }
    }
    console.log('Date header:', dateHeader);
    await route.continue();
  });

  await page.goto('https://ray.run/');
});

This test intercepts requests to https://ray.run/**, retrieves the 'Date' header value, and logs it to the console.

Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].