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

Setting user-agent via page.setUserAgent along with page.setRequestInterception does not correctly change the user-agent. #3458

Closed
rqueraud opened this issue Oct 29, 2018 · 2 comments

Comments

@rqueraud
Copy link

rqueraud commented Oct 29, 2018

Steps to reproduce

Tell us about your environment:

  • Puppeteer version: 1.9.0
  • Platform / OS version: Ubuntu 16.04
  • URLs (if applicable):
  • Node.js version: 11.0.0

What steps will reproduce the problem?

Setting user-agent via page.setUserAgent, then allow request interception via page.setRequestInterception(true) and surcharge headers with whatever you want.

Please include code that reproduces the issue.

var express = require('express');
const puppeteer = require('puppeteer');

(async () => {
    //Set small express server to print headers
    new Promise((resolve, reject) => {
        let port = 3001;
                
        let app = express();
        app.use(express.json());
        app.listen(port);
        app.route('/test').get((req, res) => {
            console.log(JSON.stringify(req.headers));
        });
    });

    //Create browser
    let puppeteerArgs = [
        '--no-sandbox',
        '--disable-setuid-sandbox',
        '--enable-features=NetworkService'
    ];
    let browser = await puppeteer.launch({
        headless: true,
        args: puppeteerArgs
    });

    //Create page and set user-agent and interception
    var page = await browser.newPage();

    await page.setUserAgent('newUserAgent');
    await page.setRequestInterception(true);
    page.on('request', request => {
        request.continue({headers: {}});
    });

    //Call the api
    await page.goto('http://localhost:3001/test');

})();

What is the expected result?
Printed headers object should contain the user-agent set by page.setUserAgent.

What happens instead?
Print headers object contains the puppeteer user-agent.
So we have to explicitely change it in the returned header of request.continue().
This should either be changed or written in the doc.

aslushnikov added a commit to aslushnikov/puppeteer that referenced this issue Nov 1, 2018
@aslushnikov
Copy link
Contributor

So we have to explicitely change it in the returned header of request.continue().

@rqueraud So what are you trying to achieve when doing request.continue({headers: {}});?

P.S. I expected a different pattern to be used with header overrides - sent a PR with an example for the documentation.

@rqueraud
Copy link
Author

rqueraud commented Nov 5, 2018

@aslushnikov With that example, I was showing that not overriding any fields in the headers still resulted to the userAgent header to be reinitialized as if no page.setUserAgent() were used.
That was not working because I wasn't following the correct pattern, as you show in your example, you have to reinject every property of the existing headers to the overriding header if you want to conserve them.
Following the new example you provided in the doc made it work. Thanks.

@rqueraud rqueraud closed this as completed Nov 5, 2018
@rqueraud rqueraud reopened this Nov 5, 2018
@rqueraud rqueraud closed this as completed Nov 5, 2018
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

No branches or pull requests

2 participants