Casperjs: How can I print http requests and responses?

Euphe picture Euphe · Jan 4, 2014 · Viewed 8.9k times · Source

For debugging purporses I need to see the whole request: headers and data. How can I achieve this?

Answer

Darren Cook picture Darren Cook · Jan 5, 2014

Casper (well, actually PhantomJS) supplies two callbacks, one when the resource is requested (where you can see headers being sent), and one when response is received (so you can see the headers the server replied with):

var utils = require('utils');

var casper = require('casper').create();
casper.options.onResourceRequested = function(C, requestData, request) {
    utils.dump(requestData.headers);
};
casper.options.onResourceReceived = function(C, response) {
    utils.dump(response.headers);
};

(Using utils module is optional, it just gives nice human-readable formatting. Thanks to thelogix and AlanChavez for the suggestion in the comments.)