Euphe
Euphe

Reputation: 3699

Casperjs: How can I print http requests and responses?

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

Upvotes: 10

Views: 9196

Answers (1)

Darren Cook
Darren Cook

Reputation: 28913

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.)

Upvotes: 20

Related Questions