Basic Authentication with LWP::UserAgent and HTTP::Request::Common
A while ago I wrote an article on LWP::UserAgent and Basic Authentication and posted it on Reddit as well, where a user pointed to an even simpler solution, one that I did not find in the documentation myself. It uses LWP::UserAgent and HTTP::Request::Common which is a dependency of LWP::UserAgent anyway.
(Actually, I think I saw and used this or a similar solution a few years ago, but I did not remember or could not find it again when I wrote the previous article. Now I seem to recall there was an issue with this solution when the request redirected to another URL that requred Basic Authentication, but I am not entirly sure. The point is that I think this solution works in most of the cases, but in the rare special cases you might still need the other solution.)
examples/basic_authentication.pl
use strict; use warnings; use v5.10; use LWP::UserAgent; use HTTP::Request::Common; my $ua = LWP::UserAgent->new(); my $request = GET 'https://pause.perl.org/pause/authenquery'; $request->authorization_basic('szabgab', '*******'); my $response = $ua->request($request); say $response->as_string();
Debugging the script
If you have problems running the script, there are some nice techniques to make UserAgent Debugging Easy.
Published on 2016-09-28