Ease up HTTP-GET with perl's LWP::UserAgent

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:...

2012-12-21 · 1 min · 143 words · Jan