Skip to content

Instantly share code, notes, and snippets.

Last active February 20, 2017 02:14
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Download ajax calls responses for a set of pages

Download ajax calls responses for a set of pages

The need

Have a demo copy of our frontend with static data.

The solution

I wanted a reusable solution, something I could pass a set of URLs, something that could track each ajax request and give me access to the payload. That something is CasperJS.

The hook I was looking for is an event called resource.received (docs), its callback receives a resource object which comes from PhantomJS as CasperJS just forwards the event (docs).

Here is the simplified version of the code I've used, with bonus code for the login:

var fs = require('fs');
var casper = require('casper').create({
  verbose: false,
  logLevel: 'debug',
  webSecurityEnabled: false
casper.on('remote.message', function(msg) {
  this.echo('remote message caught: ' + msg);
var endpoints = [];
casper.on('resource.received', function(resource) {
  var status = resource.status;
  if (/\/api\/1/.test(resource.url) && endpoints.indexOf(resource.url) == -1) {

casper.start('', function() {
  casper.waitForSelector('body', function() {
    this.fillSelectors("form[name='login']", {
      'input[name=username]': 'root',
      'input[name=password]': 'toor'
    }, true);

casper.then(function() {
  casper.cli.args.forEach(function(arg) {
    casper.thenOpen('' + arg, function() {});

casper.then(function() {
  endpoints.forEach(function(endpoint) {
    this.echo(endpoint);, 'json' + fs.separator + filename(endpoint));

function filename(endpoint) {
  return endpoint
    .replace('', '')
    .replace(/(.*)\?.*/, '$1')
    .replace(/\//g, '.');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment