Adding a Custom Http Header Using CasperJS

I was working with some web tests using CasperJS, and got the following error from the server (IIS):

HTTP Error 500.52 – URL Rewrite Module Error.
Outbound rewrite rules cannot be applied when the content of the HTTP response is encoded (“deflate”).

I couldn’t change the server configurations, so I had to force the server to return unencoded (uncompressed) content. It turned out to be quite hard to find a solution, but the solution was just three lines of code.

In theory, setting this in the options argument to Casper should work:

var casper = require(‘casper’).create(
{
    pageSettings: {
        customHeaders: {
            ‘Accept-Encoding’: ‘identity’
        }
    }
});

But it didn’t.

Nor did passing the headers option to open(http://docs.casperjs.org/en/latest/modules/casper.html?highlight=header#open). The problem with that approach was that the header was only set to the first request, not for subsequent requests as a result of redirects.

This conversation inspired me to come up with my solution: https://github.com/n1k0/casperjs/issues/667. I simply hooked up the start event and set the header:

casper.on(‘started’, function () {
    this.page.customHeaders = { ‘Accept-Encoding’: ‘identity’ }
});

Advertisements

One thought on “Adding a Custom Http Header Using CasperJS”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s