How to get http and https return code in perl?

by elisha_langworth , in category: Third Party Scripts , 4 months ago

How to get http and https return code in perl?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by jasen_gottlieb , 4 months ago

@elisha_langworth 

You can use the HTTP::Request and LWP::UserAgent modules in Perl to send HTTP requests and get the response codes. Here is an example code snippet that shows how to get the HTTP and HTTPS return codes:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
use LWP::UserAgent;

my $url = "http://example.com";

my $ua = LWP::UserAgent->new;
my $response = $ua->get($url);

if ($response->is_success) {
    print "HTTP return code: " . $response->code . "
";
} else {
    print "HTTP return code: " . $response->code . "
";
}


In this code, we first create a new LWP::UserAgent object, then send a GET request to the specified URL using the get method. We then check if the response is successful using the is_success method and print out the HTTP return code using the code method.


You can do the same for HTTPS URLs by simply changing the URL to an HTTPS URL in the code.