How do I set multiple custom HTTP headers in puppeteer?

Viewed 14532

I am trying to login using puppeteer at https://kith.com/account/login?return_url=%2Faccount When I login and solve the captcha with audio, it detects me as a bot, so I am trying to change the request headers to see if that helps but cannot find anything on how to change them.

I found this, but it only shows 1 header:

await page.setRequestInterception(true)

        page.on('request', (request) => {
            const headers = request.headers();
            headers['X-Just-Must-Be-Request-In-All-Requests'] = '1';
            request.continue({
                headers
            });
        });
2 Answers

You are able to set multiple HTTP headers with the dedicated puppeteer method: page.setExtraHTTPHeaders as well.

E.g.:

await page.setExtraHTTPHeaders({
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36',
        'upgrade-insecure-requests': '1',
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'en-US,en;q=0.9,en;q=0.8'
    })
await page.goto('...')

header is array you can add many as you want

 page.on('request', (request) => {
            const headers = request.headers();
            headers['X-Just-Must-Be-Request-In-All-Requests'] = '1';
            headers['foo'] = 'bar';
            headers['foo2'] = 'bar2';
            
            request.continue({
                headers
            });
        });
Related