2

When I use LWP::UserAgent to make an Authenticated request there are two requests made to my server.

The docs says:

The request method will process redirects and authentication responses transparently. This means that it may actually send several simple requests via the "simple_request" in LWP::UserAgent method.

I know I can use authorization_basic to add Authorization header.

$req->authorization_basic('user', 'pass');                                  

But is there any way to make just one request using only LWP::UserAgent?

I tried to create a Response with 401 code to pass as the previous argument but simple_request is called before this.

Minimal Reproducible Example:

server.pl

use strict;
use warnings;

use HTTP::Daemon;
use HTTP::Status;

my $server = HTTP::Daemon->new(LocalAddr => '127.0.0.1', LocalPort => 9999) or die;

print "Contact URL: ", $server->url, "\n";
while (my $connection = $server->accept) {
    while (my $request = $connection->get_request) {
        print $request->as_string;
        unless( $request->header( 'Authorization' ) ) {
            $connection->send_response( make_challenge() )
            }
        else {
            $connection->send_response( make_response() )
            }
        }
    $connection->close;
    }

sub make_challenge {
    my $response = HTTP::Response->new(
        401 => 'Authorization Required',
        [ 'WWW-Authenticate' => 'Basic realm="Test-Realm"' ],
         );
    }

sub make_response {
    my $response = HTTP::Response->new(
        200 => 'Huzzah!',
        [ 'Content-type' => 'text/plain' ],
         );

    $response->message( 'Huzzah!' );
    }

client.pl

#!/usr/bin/env perl                                                             

use strict;                                                                     
use warnings;                                                                   
use LWP;                                                                        

my $ua = LWP::UserAgent->new('Mozilla');
my $req = HTTP::Request->new(GET => 'http://127.0.0.1:9999/');                                        
$ua->credentials("127.0.0.1:9999", "Test-Realm", 'user_name', 'some_pass');                       
my $res = $ua->request($req);               

print $res->content;

Output:

Run Server:

$ perl server.pl
Contact URL: http://127.0.0.1:9999/

Run Client:

$ perl client.pl
HTTP/1.1 Huzzah! 
Date: Tue, 29 Mar 2022 17:39:53 GMT
Server: libwww-perl-daemon/6.12
Connection: close

Server Output:

GET / HTTP/1.1
Connection: TE, close
Host: 127.0.0.1:9999
TE: deflate,gzip;q=0.3
User-Agent: libwww-perl/6.52

GET / HTTP/1.1
Connection: TE, close
Authorization: Basic dXNlcl9uYW1lOnNvbWVfcGFzcw==
Host: 127.0.0.1:9999
TE: deflate,gzip;q=0.3
User-Agent: libwww-perl/6.52

Notes:

Example adapted from here.

wiltonsr
  • 639
  • 5
  • 16
  • Thanks for reply @ikegami. Could you provide an answer with a snippet of code? – wiltonsr Mar 29 '22 at 18:33
  • I'm looking for some way to the `LWP::UserAgent` does this dirty job. Passing the `Authorization` hard coded is not an option. – wiltonsr Mar 29 '22 at 19:02
  • @ikegami How can I do this without hard coding it? I updated the question to use `$ua->request($req);` – wiltonsr Apr 20 '22 at 15:10

0 Answers0