Reputation: 19562
What is the easiest/best way to access an https address requiring username/password and just check the HTTP response (if it is 3xx/4xx/5xx)?
Is it possible to do it without installing extra modules?
The idea behind this is just to do a health check (automatic) to an internal server. The page requires username/password which I have
Upvotes: 0
Views: 253
Reputation: 54323
If you want it out of the box, you can just use LWP::UserAgent and create your own HTTP::Request object, which has an authorization_basic
method. It's a bit hard to find in the documentation. It comes from HTTP::Message, which says it delegates some methods to HTTP::Headers.
All methods unknown to HTTP::Message itself are delegated to the HTTP::Headers object that is part of every message. This allows convenient access to these methods. Refer to HTTP::Headers for details of these methods
For coveniance you may use HTTP:Request::Common.
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common;
my $req = GET 'https://www.example.org';
$req->authorization_basic('user', 'password');
my $ua = LWP::UserAgent->new;
my $res = $ua->request($req);
print $res->code;
Output:
200
If you are not interested in the content, maybe a HEAD request is enough. That will save some time as no body is sent over the network.
my $req = HEAD 'https://www.example.org';
Upvotes: 2
Reputation: 6602
Try using WWW::Mechanize
#!/usr/bin/env perl
use strict;
use warnings;
use IO::Socket::SSL;
use WWW::Mechanize;
my $mech = WWW::Mechanize->new(
# Need ssl_opts if trying to get past an invalid certificate...
ssl_opts => {
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE,
verify_hostname => 0,
},
# Do not die if the page fetch fails
autocheck => 0,
);
# user, password
$mech->credentials( 'robot_user', 'r0B07_U$3r_p4$$\/\/0rD' );
$mech->get("https://foo.bar.baz.com/secret_url.html");
if ($mech->success) {
print "==> SUCCESS\n";
print $mech->content();
} else {
print "==> FAILURE\n";
print "HTTP STATUS [ " . $mech->status . " ]\n";
}
Upvotes: 2