I was just writing a small script for automatizing something via a REST-API and I had to manually add an authorization to all my GET-Requests, which really annoyed me. (it was not Realm-sensitive, so I couldn’t set up auth in the UA itself).

So I wrote this small sub I would like to share:

#getrequests shorthand
sub getReq {
   my ( $url, $username, $password ) = @_;
   my $req = HTTP::Request->new( GET => $url );
   if ( $username && $password ) {
      $req->authorization_basic( $username, $password );
   }
   my $res = $ua->request($req);
   if   ( $res->is_success ) { return $res; }
   else                      { warn $res->status_line; return undef; }
}

All you need to call a HTTP-Base-Auth’d site now is:

Or, if you don’t need Auth, you can still use that, with just not defining username/password:

To check if your request failed or not, just run: