Mercier
Mercier

Reputation: 96

PhantomJS, CasperJS - requests, responses and file download

I want to see the communication flow while accessing a site.

So far I was using page.onResourceRequested and page.onResourceReceived. Example:

page.onResourceReceived = function(R) {

    console.log(R.id+' '+R.status+' '+R.contentType+' '+R.redirectURL);
};

This worked as a charm. Problem: it appeared that I had to have ability to download certain files (i.e. see the body of responses, be that an image or a javascript file).

Turns out version 1.8.2 does not have this functionality and it is unclear whether 1.9 will. So... CasperJS has "download" method.

But porting PhantomJS to casperjs turns out to be tricky. E.g. I can use:

casper.on("http.status.301", function(resource) {
    this.echo(resource.url + " is permanently redirected", "PARAMETER");
}); 

from casperjs's sample events.js

This is actually a phantomjs response... But I must monitor all requests/responses

So I tried doing:

var casper = require("casper").create(
  {
    verbose: true,
    logLevel: "debug",
    exitOnError: error,
    onResourceReceived: resRecv,
    onResourceRequested: resReq,
}


);

With: function resRecv(R) {

console.log( R.id+' '+R.status+' '+R.contentType+' '+R.redirectURL);

}

Which didn't work because R is not a response? How can I get the same parameter as in 'casper.on("http.status.301" ...'?

Or maybe I'm wrong and PhantomJS can download files after all?

Upvotes: 2

Views: 4283

Answers (3)

fearless_fool
fearless_fool

Reputation: 35149

This is a late answer and perhaps not what you were after, but I've found a web proxy such as Charles to be an invaluable tool while debugging PhantomJS / CasperJS scripts. It sits between the browser and the remote site(s) and reports all requests and responses. And it's SSL savvy so it shows HTTPS exchanges in plain text.

The only caveat is that you may need to run with ignore-ssl-errors set to true, as in:

casperjs --ignore-ssl-errors=true --proxy=127.0.0.1:8888 ./casper-test.js

Upvotes: 0

nekoflux λ
nekoflux λ

Reputation: 51

Which version of CasperJS are you using? The 1.1 docs indicate that the first argument passed to onResourceReceived and onResourceRequested is a reference to the casper object itself. This worked for me:

 casper.options.onResourceRequested = function(R, req) { ... }

where req has the object properties you are looking for.

Upvotes: 1

user2187553
user2187553

Reputation: 1

But I think it only gets type 3** redirections, and not consider webpages which have meta tag redirection and redirection through javascript.

Upvotes: 0

Related Questions