perl http request
To make an HTTP request in Perl, you can use the LWP::UserAgent
module. Here's an example code snippet:
use LWP::UserAgent;
my $url = 'https://example.com';
my $ua = LWP::UserAgent->new;
my $response = $ua->get($url);
if ($response->is_success) {
print $response->content;
} else {
print "Error: " . $response->status_line;
}
In this code, we first create a LWP::UserAgent
object. Then, we use the get
method of the user agent to make an HTTP GET request to the specified URL. The response is stored in the $response
variable. We can check if the request was successful using the is_success
method of the response object. If it was successful, we can access the content of the response using the content
method. If there was an error, we can get the status line using the status_line
method.
Please note that you may need to install the LWP::UserAgent
module if it's not already installed on your system. You can do this by running the following command:
cpan LWP::UserAgent