station
station

Reputation: 7145

Phantomjs check for response headers and then execute something

I have the following RequestURL.js file.

var webPage = require('webpage');
var system = require('system');
var page = webPage.create();

page.customHeaders = {"pragma": "akamai-x-feo-trace"};
page.settings.userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"

if (system.args.length === 1) {
    console.log('Try to pass some args when invoking this script!');
} else {
    page.open(system.args[1], function (status) {
    var content = page.content;
    console.log(content);
    phantom.exit();
    });
}

Now I execute this as phantomjs --ignore-ssl-errors=yes --ssl-protocol=any RequestURL.js #my_url_here > body.html

Now I have a parser written in python that takes body.html and executes it. Now before that I want the page source to get generated only if the response contains the following header.

X-Akamai-FEO-State:TRANSFORMING

Is there a way to modify my RequestURL.js to get there.

Upvotes: 0

Views: 2920

Answers (1)

Artjom B.
Artjom B.

Reputation: 61892

It is expected that page.onResourceReceived is triggered before the page.onLoadFinished callback of page.open().

var transforming = false;
page.onResourceReceived = function(response){
    if (response.url === system.args[1]) { // TODO handle redirects if necessary
        response.headers.forEach(function(header){
            if(header.name === 'X-Akamai-FEO-State') {
               transforming = header.value === 'TRANSFORMING';
            }
        });
    }
};

page.open(system.args[1], function (status) {
    if (transforming) {
        console.log(page.content);
    }
    phantom.exit();
});

Upvotes: 3

Related Questions