Jaimesg
Jaimesg

Reputation: 33

Casperjs/Phantomjs Modifying headers

i'm trying to change some headers but nothing is working:

var casper = require('casper').create({ // 
    stepTimeout: 15000, 
    verbose: false, 
    logLevel: 'error', 
    pageSettings: { 
        loadImages: true, 
        loadPlugins: true, 
        userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.364',
        customHeaders: { 
            Connection: 'keep-alive', 
            Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' 
} } });

I also tried:

phantom.page.customHeaders = { 
    "User-Agent" : "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:26.0) Gecko/20100101 Firefox/26.0", 
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 
    "Accept-Language": "en-US,en;q=0.5", "Accept-Encoding": "gzip, deflate",
    "Connection" : "keep-alive" };

And for a single connection:

this.open('http://localhost/post.php', { 
    method: 'post', 
    headers: { 'Accept': 'application/json' } 
});

None of them are working or am i doing something wrong? Thanks

Upvotes: 3

Views: 1820

Answers (2)

artificiality
artificiality

Reputation: 1

Maybe a late one, but a more elegant solution is making use of the .thenOpen function.

function thenOpen(location, settings, then)

While not providing a location URL for the .start function. An example would look like this:

var customHeaders = { "Cache-Control": "max-age=0" };
var casper = require('casper').create();
    
casper.start(  function() {
     });
    
casper.thenOpen( param_url , { headers: customHeaders }, function() {
      this.echo(this.getTitle());
    });
    
casper.run();

Upvotes: 0

Badacadabra
Badacadabra

Reputation: 8497

I cannot reproduce your problem. It seems to work for me... Maybe you have an issue with a redirection somewhere, like discussed here.

May I suggest you to do like this guy and try the following code?

casper.on('started', function () {
  this.page.customHeaders = {
    "User-Agent" : "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:26.0) Gecko/20100101 Firefox/26.0", 
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 
    "Accept-Language": "en-US,en;q=0.5",
    "Accept-Encoding": "gzip, deflate",
    "Connection" : "keep-alive"
  }
});

Upvotes: 5

Related Questions