Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: get extra headers from Fetch.requestPaused event #8162

Merged
merged 1 commit into from Mar 28, 2022

Conversation

Junyan
Copy link
Contributor

@Junyan Junyan commented Mar 24, 2022

What kind of change does this PR introduce?

  • merge request headers between Fetch.requestPaused and Network.requestWillBeSent

Did you add tests for your changes?

  • Yes

Summary

extra headers from Fetch.requestPaused

image

Does this PR introduce a breaking change?

  • Yes, httpRequest.headers() return more headers.

Other information

A base case with CORS error.

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({
    headless: false,
    devtools: true,
    defaultViewport: null,
  });
  const page = (await browser.pages())[0] || await browser.newPage();

  await page.setRequestInterception(true);

  page.on('request', (request) => {
    const headers = request.headers();

    request.continue({
      headers,
    });
  });

  await page.goto('https://www.tiktok.com/?lang=en');
})();

A patch to fix it.

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({
    headless: false,
    devtools: true,
    defaultViewport: null,
  });
  const page = (await browser.pages())[0] || await browser.newPage();
  const pausedRequestMap = new Map();

  await page.setRequestInterception(true);

  page._client.on('Fetch.requestPaused', (event) => {
    const { networkId } = event;

    pausedRequestMap.set(networkId, event);
  });

  page.on('request', (request) => {
    const headers = request.headers();
    const pausedRequest = pausedRequestMap.get(request._requestId);

    if (pausedRequest) {
      Object.assign(headers, pausedRequest.request.headers);
    }
  
    request.continue({
      headers,
    });
  });

  await page.goto('https://www.tiktok.com/?lang=en');
})();

@OrKoN
Copy link
Collaborator

OrKoN commented Mar 25, 2022

Thanks for the PR. It looks great! Let me just clarify the situation with those headers upstream before landing.

@OrKoN OrKoN force-pushed the fix-request-event-headers branch from b23482a to 4eb4164 Compare March 25, 2022 06:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants