How to make HTTP GET request in Perl?
Question
How to make HTTP GET request in Perl?
Making an HTTP GET request in Perl is a common task in web programming and API consumption. Perl offers multiple ways to perform HTTP requests, but the most straightforward and powerful approach is to use the LWP::UserAgent module, which is part of the core Perl modules (since Perl 5.8, and widely available). This module provides a full-featured web client supporting GET, POST, and other HTTP methods.
Using LWP::UserAgent for HTTP GET Requests
LWP::UserAgent allows you to create a user agent object, set headers, send requests, and handle responses efficiently. Here is a simple example demonstrating an HTTP GET request to fetch the content of a webpage and print the status and response content.
use strict;
use warnings;
use LWP::UserAgent;
# Create a new user agent object
my $ua = LWP::UserAgent->new;
# Define a target URL for the GET request
my $url = 'http://httpbin.org/get';
# Perform the GET request
my $response = $ua->get($url);
# Check if the request was successful
if ($response->is_success) {
print "Success! Response content:\n";
print $response->decoded_content, "\n"; # decoded_content handles charset automatically
} else {
print "HTTP GET error: ", $response->status_line, "\n";
}
Explanation and Perl Concepts
LWP::UserAgentis the main class to create HTTP clients.- The method
get($url)performs an HTTP GET request and returns anHTTP::Responseobject. - The
is_successmethod checks if the HTTP request was successful (status code 200). decoded_contentreturns the content in decoded form based on the response header charset, a handy feature.- Perl’s sigils (like
$for scalars, here$uaand$response) indicate data types clearly. - Strict and warnings pragmas are enabled for safer, cleaner coding.
Common Pitfalls
- Not checking
$response->is_successmay lead to silent failures. - Using
contentinstead ofdecoded_contentcan cause issues if the response is encoded (e.g., gzip, charset). - Make sure you have
LWP::UserAgentinstalled; it is core but older systems may lack it. - Beware of redirects:
LWP::UserAgentfollows redirects by default, but you can configure this behavior.
Alternative Modules
For newer code, modules like HTTP::Tiny (core since Perl 5.14) offer a simpler, lightweight interface, but LWP::UserAgent remains the most full-featured for complex cases.
This example runs as-is and prints the HTTP GET response. Just save it to a file or run via command line:
perl yourscript.pl
Or directly from the command line:
perl -MLWP::UserAgent -e 'my $ua = LWP::UserAgent->new; my $response = $ua->get("http://httpbin.org/get"); print $response->is_success ? $response->decoded_content : $response->status_line;'
Thus, LWP::UserAgent with get is a reliable and idiomatic way to make HTTP GET requests in Perl.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 1222ms
Success! Response content:
{
"args": {},
"headers": {
"Host": "httpbin.org",
"User-Agent": "libwww-perl/6.44",
"X-Amzn-Trace-Id": "Root=1-695369fd-52738e992887352e7eafffb3"
},
"origin": "222.213.244.47",
"url": "http://httpbin.org/get"
}
(empty)