Search code examples
perlhttprequestlwp

Perl and LWP not authenticating


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.


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.