yoav barnea
yoav barnea

Reputation: 5994

Add a request header through the event: page.resource.requested in CasperJS

I tried (without success) to add a custom header to the request through the event:

 casper.on("page.resource.requested",function(requestData,request){...}

something like that:

casper.on("page.resource.requested",function(requestData,request){

     request.setHeader("X-myHeader", "123");
}

I thought I could do that because in PhantomJS you could write:

 page.onResourceRequested = function(requestData, networkRequest) {
      networkRequest.setHeader("X-myHeader", "123");
 }

What are my options to add a request header during that event (of the casper object)?

Upvotes: 2

Views: 807

Answers (1)

Artjom B.
Artjom B.

Reputation: 61892

In the latest version 1.9.7, there is a bug request in onResourceRequested has no setHeader function #12264. You need to use an earlier version of phantomjs.

The difference arises because you use a different version of phantomjs on the commandline and in casperjs. In windows you find casper's phantom in this folder:

C:\Users\someuser\AppData\Roaming\npm\node_modules\casperjs\node_modules\phantomjs

The two easiest actions you could do is, either

  • add the working phantomjs that you can use to your PATH (casper will use it)

    OR

  • overwrite the executable inside casperjs\node_modules\phantomjs\lib\phantom with the working phantomjs version

    OR

  • just let npm download the version for you

The steps for that last point are:

  1. Find out which version of phantomjs it works with by calling phantomjs --version from commandline

  2. Change into the following folder in commandline

    C:\Users\someuser\AppData\Roaming\npm\node_modules\casperjs
    
  3. Install your working version (I don't actually know which one works, so I use 1.9.0 here)

    npm install [email protected]
    

    You can check what version are available by calling npm show phantomjs.

Upvotes: 3

Related Questions