question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Puppeteer/chrome does not follow/show valid 302 redirects when the final URI is not known

See original GitHub issue

We are using Puppeteer to verify that advertiser links are redirecting to the play store as they are supposed to. Advertisers use a combination of 302 server redirects and front-end javascript redirects (with 200 response codes), making Puppeteer a great choice to evaluate final redirect destinations. The issue is that 302 redirects are not being redirected to in Puppeteer.

Steps to reproduce

What steps will reproduce the problem?

(async () => {
        const browser = await puppeteer.launch();
        const page = await browser.newPage();
        await page.emulate(determineDevice(device)); // returns the correct device from DeviceDescriptors.js

        const finalResponse = await page.goto("http://appclk.me/store.php?page=1", {waitUntil: 'networkidle0'});

        process.stdout.write(JSON.stringify({
            success: true,
            statusCode: finalResponse.status,
            url: finalResponse.url
        }));

        process.exit(0);
})();

What is the expected result?

The example URL used, http://appclk.me/store.php?page=1 returns a 200 status code that uses meta and javascript redirects that point to http://appclk.me/store.php?page=2. This subsequent URL redirects with a 302 to a market link: market://details?id=com.kabam.marvelbattle. What SHOULD happen is one of the following:

  • The final URL after the network is idle should be http://appclk.me/store.php?page=2 with a status code of 302. We could use the Response headers to verify that it redirects to the market link.
  • OR The final URL after network is idle should be market://details?id=com.kabam.marvelbattle (though Chrome doesn’t know how to handle this URL.)

What happens instead?

The final URL that is printed is http://appclk.me/store.php?page=1 with a status code of 200.

Notes

We are seeing the same behavior in Chrome: open a new tab with the Network dev tools open and visit the first link. You will see that the second redirect is never shown in the network tab. This is strange behavior as the network tab should show all requests and responses. Other browsers like FF do show the second request in the Network tab.

I’m aware that this is something with Chrome and not Puppeteer, but I’m posting here in the hopes that someone will point out a flag or some option I’m not aware of that correctly shows ALL network requests so that we can accomplish our goal of verifying redirects properly. Any suggestions are welcome.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

1reaction
eblansheycommented, Nov 22, 2017

I found the solution, which is to use page.setRequestInterception()

        const browser = await puppeteer.launch();
        const page = await browser.newPage();
        await page.emulate(determineDevice(device));
        await page.setRequestInterception(true);

        page.on('request', request => {
            console.log('GOT NEW REQUEST', request.url);
            request.continue();
        });

        page.on('response', response => {
            console.log('GOT NEW RESPONSE', response.status, response.headers);
        });

What’s interesting here is that if I comment out await page.setRequestInterception(true); but still leave the request/response event listeners, the second request/response is NOT printed to the console. If setRequestInterception IS used, then THREE requests are logged, with the second correctly displaying a 302, and the third being the “market://” link. Why would requesting to intercept requests change the events that are fired? Is this a bug?

0reactions
elainemacommented, Jun 10, 2019
emulate

sometimes, the program goes to the event page.requestfailed rather than page.response, so can’t handle the 302 status, do you hava any solutions ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

URLs that 302 Redirect to the Play Store with "market://details ...
What I'm ACTUALLY trying to do is make sure some advertiser's links are redirecting to the play store. So the network tab will...
Read more >
How To Fix the HTTP 302 Error (5 Methods) - Kinsta
1. Determine Whether the Redirects Are Valid. 302 responses are usually not errors. Temporarily redirecting users to a different page can be a ......
Read more >
302 Found - HTTP - MDN Web Docs
The HyperText Transfer Protocol (HTTP) 302 Found redirect status response code indicates that the resource requested has been temporarily ...
Read more >
302 Found: What It Is and How to Fix It - Airbrake Blog
A 302 Found message is an HTTP response status code indicating that the requested resource has been temporarily moved to a different URI....
Read more >
SVR1: Implementing automatic redirects on the server side ...
Server-side technologies provide methods to implement redirects in a way that does not confuse users. A server-side script or configuration file can cause ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found