Question

I'm trying to get an LWP request working to an https server. I have been given a user & pass, advised to use basic authentication. I've tried various chunks of code, and all seem to get an authentication error. My current code is...

use warnings;
use strict;
use Data::Dumper;
use LWP;

my $ua = LWP::UserAgent->new( keep_alive => 1 );
##also tried by $ua->credentials('domain','','user','pass');
##not sure if I need 'realm' or how I get it, as no popup on screen.
my $request = HTTP::Request->new( GET => "https://my.url.com/somepath/" );
$request->authorization_basic('myuser','mypass');
$request->header( 'Cache-Control' => 'no-cache' );

print $response->content;
print Dumper $response;

The server gives a security error, but if I look at a dump of $response, I see the following...

'_rc' => '401',
'_headers' => bless( {   .... lots of stuff
        'title' => 'Security Exception',
        'client-warning' => 'Missing Authenticate header',
        'client-ssl-socket-class' => 'IO::Socket::SSL',
         ...
        'expires' => '-1'
                                  }, 'HTTP::Headers' ),

'_msg' => 'Unauthorized',
'_request' => bless( {
     '_content' => '',
     '_uri' => bless( do{\(my $o = 'https:theurlabove')}, 'URI::https' ),
     '_method' => 'GET',
     '_uri_canonical' => $VAR1->{'_request'}{'_uri'}
     '_headers' => bless( {
                           'user-agent' => 'libwww-perl/6.04',
                           'cache-control' => 'no-cache',
                           'authorization' => 'Basic dzx..........'
                         }, 'HTTP::Headers' ),

I'm trying to understand whats happening, it looks like in the original request, it has the headers in there, but in the response, its saying I'm 'Missing Authenticate Header'.

Is there something amiss with the code, or something I'm misunderstanding with the request/respinse ?

Thanks.

Was it helpful?

Solution

The "Missing Authenticate header" message is coming from LWP itself. This means that it couldn't find an authenticate header in the target response. This might mean that your proxy settings are misconfigured, if you have anything like that.

OTHER TIPS

I don't know if this is what you are looking for but I came across the same problem trying to authenticate to a webpage and had to solve it with WWW::Mechanize. I had to go to the first page and login then request the page I wanted.

use WWW::Mechanize;


my $loginPage = "http://my.url.com/login.htm";          # Authentication page
my $mech = WWW::Mechanize->new();                       # Create new brower object
$mech->get($loginPage);                                 # Go to login page
$mech->form_name('LogonForm');                          # Search form named LogonForm
$mech->field("username", myuser);                       # Fill out username field
$mech->field("password", mypass);                       # Fill out password field
$mech->click("loginloginbutton");                       # submit form
$mech->get("http://my.url.com/somepath/");              # Get webpage
# Some more code here with $mech->content()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top