I am using CapserJS 1.1.0-beta3 combined with PhantomJS 1.8.2.
I call an url which respond with a redirect (HTTP 302). PhantomJS automatically follows the redirect, but in my usecase PhantomJS should not follow the redirect.
The debug output of the redirect looks like:
[debug] [phantom] Navigation requested: url=https://foo.com/bar.jsp, type=Other, willNavigate=true, isMainFrame=true
How can I configure PhantomJS/CapserJS to not follow redirects?
There is a little workaround necessary. So you need to first identify which URL is the redirect. With resource.received
you receive the response to the first request which contains the URL where it should be redirected to. But we can't do anything from this event handler. We therefore save the target URL which is identified as a redirect target for later.
Now the underlying headless browser (PhantomJS or SlimerJS) follows the redirect by requesting the new resource, but now resource.requested
provides us with the tools to abort the request (sadly this is not documented in CasperJS). So the final script looks like this:
var casper = require("casper").create();
var redirectURLs = [],
doLog = true;
casper.on("resource.requested", function(requestData, networkRequest){
if (doLog) console.log('Request (#' + requestData.id + '): ' + JSON.stringify(requestData) + "\n");
if (redirectURLs.indexOf(requestData.url) !== -1) {
// this is a redirect url
networkRequest.abort();
}
});
casper.on("resource.received", function(response){
if (doLog) console.log('Response (#' + response.id + ', stage "' + response.stage + '"): ' + JSON.stringify(response) + "\n");
if (response.status === 301) { // use your status here
redirectURLs.push(response.redirectURL);
}
});
casper.start("https://stackoverflow.com/q/27021176").run(function(){
this.echo("DONE");
this.exit();
});
This is adapted from my answer A: How to configure Poltergeist or PhantomJS to not follow redirects?
You could do the same as with the linked PhantomJS version directly in CasperJS by exchanging page
for casper.page
, but CasperJS has a few advantages. You can add multiple handlers to the same events with the casper.on
notation and most all you can decide if all resources are treated the same way or just page loads. So you can exchange resource.received
for page.resource.received
and resource.requested
for page.resource.requested
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With