networking intermediate

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::UserAgent is the main class to create HTTP clients.
  • The method get($url) performs an HTTP GET request and returns an HTTP::Response object.
  • The is_success method checks if the HTTP request was successful (status code 200).
  • decoded_content returns the content in decoded form based on the response header charset, a handy feature.
  • Perl’s sigils (like $ for scalars, here $ua and $response) indicate data types clearly.
  • Strict and warnings pragmas are enabled for safer, cleaner coding.

Common Pitfalls

  • Not checking $response->is_success may lead to silent failures.
  • Using content instead of decoded_content can cause issues if the response is encoded (e.g., gzip, charset).
  • Make sure you have LWP::UserAgent installed; it is core but older systems may lack it.
  • Beware of redirects: LWP::UserAgent follows 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

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
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"
}

STDERR
(empty)

Was this helpful?

Related Questions