Skip to content Skip to sidebar Skip to footer

Phantomjs Doesn't Send Authentication Header

I'm trying to open a web page which requires HTTP authentication, in PhantomJS. My script is based off the loadspeed.js example: var page = require('webpage').create(), t, addr

Solution 1:

PhantomJS (at least as of 1.9.0) has a bug with auth: it sends the request without the auth headers, and then only after it gets the 401 back does it do the request again but this time with the headers. (That is for GET; with POST it doesn't work at all.)

The workaround is simple, so instead of:

page.settings.userName = 'username';page.settings.password = 'password';

you can use:

page.customHeaders={'Authorization': 'Basic '+btoa('username:password')};

(I just covered this in a blog post: http://darrendev.blogspot.jp/2013/04/phantomjs-post-auth-and-timeouts.html, and learnt that workaround on the PhantomJS mailing list from Igor Semenko.)

Solution 2:

I dont think there is anything wrong with the script your using or phantomjs (at least in v1.5).

If you try this script:

var page = require('webpage').create(),
    system = require('system'),
    t, address;

page.settings.userName = 'test';
page.settings.password = 'test';

if (system.args.length === 1) {
    console.log('Usage: loadspeed.js <some URL>');
    phantom.exit();
} else {
    t = Date.now();
    address = system.args[1];
    page.open(address, function(status) {
        if (status !== 'success') {
            console.log('FAIL to load the address');
        } else {
            t = Date.now() - t;
            console.log('Page title is ' + page.evaluate(function() {
                return document.title;
            }));
            console.log('Loading time ' + t + ' msec');
        }
        phantom.exit();
    });
}

phantomjs loadspeed.js http://browserspy.dk/password-ok.php

The auth is successful.

Post a Comment for "Phantomjs Doesn't Send Authentication Header"