Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Puppeteer get request redirects

Is there any way to use puppeteer to get the redirects with the response body (if there are any) of the request?

I implemented the following code but I can't find a way to get the redirects...

const page = await browser.newPage()

page.on('request', (data) => console.log(data));    

await page.on('response', response => {
    const url = response.url();
    response.buffer()
    .then (
        buffer => {
            bufferString = buffer.toString();         
        },
        error => {
          console.log(error)
        }
    )
})

await page.goto('https://www.ford.com', {waitUntil: 'networkidle0'});
like image 226
Valip Avatar asked Feb 26 '18 10:02

Valip


3 Answers

Just check response.status() in your response handler - it will be 3xx for redirects:

page.on('response', response => {
  const status = response.status()
  if ((status >= 300) && (status <= 399)) {
    console.log('Redirect from', response.url(), 'to', response.headers()['location'])
  }
})

(Redirects don't usually have anything interesting in the response body, so you don't probably want to call response.buffer() for them.)

like image 117
Pasi Avatar answered Oct 18 '22 14:10

Pasi


This should work for every type of redirection: server-side and client-side (meta-refresh, JS, etc)

const page = await browser.newPage()

const redirects = [];

const client = await page.target().createCDPSession();
await client.send('Network.enable');
await client.on('Network.requestWillBeSent', (e) => {
    if (e.type !== "Document") {
        return;
    }
    redirects.push(e.documentURL);
});

await page.goto('https://www.ford.com');
await page.waitForNavigation();

console.log(redirects);

Something like that.

like image 4
Vladan Avatar answered Oct 18 '22 14:10

Vladan


You need to check the response url, because in the case of a redirect, multiple URLs could be called and I think only the first one is a 301.

Note that expect in my code is from the chai library, but that's optional, assertion could be done with something else.

const webpage = await browser.newPage();
webpage.once('response', response => {
  if (response.url() === 'https://www.ford.com') {
    expect(response.status()).equals(301);
  }
})
await webpage.goto('https://www.ford.com');
like image 1
Thomas Amar Avatar answered Oct 18 '22 14:10

Thomas Amar