How to send HTTP GET or POST request in Perl

In Perl, there are many ways to make HTTP requests by using existing Perl modules. In this tutorial, I will show examples of sending HTTP requests in Perl by using LWP Perl module. The code snippets presented here illustrate how to set HTTP request header fields, as well as data enclosed in a request body.

In order to use LWP Perl module, you need to install it first.

To install LWP on Ubuntu or Debian:

$ sudo apt-get install libwww-perl

To install LWP on CentOS, Fedora or RHEL:

$ sudo yum install perl-libwww-perl.noarch

HTTP GET Perl example

use LWP::UserAgent;

my $ua = LWP::UserAgent->new;

my $server_endpoint = "http://192.168.1.1:8000/service";

# set custom HTTP request header fields
my $req = HTTP::Request->new(GET => $server_endpoint);
$req->header('content-type' => 'application/json');
$req->header('x-auth-token' => 'kfksj48sdfj4jd9d');

my $resp = $ua->request($req);
if ($resp->is_success) {
    my $message = $resp->decoded_content;
    print "Received reply: $message\n";
}
else {
    print "HTTP GET error code: ", $resp->code, "\n";
    print "HTTP GET error message: ", $resp->message, "\n";
}

HTTP POST Perl example

use LWP::UserAgent;

my $ua = LWP::UserAgent->new;

my $server_endpoint = "http://192.168.1.1:8000/service";

# set custom HTTP request header fields
my $req = HTTP::Request->new(POST => $server_endpoint);
$req->header('content-type' => 'application/json');
$req->header('x-auth-token' => 'kfksj48sdfj4jd9d');

# add POST data to HTTP request body
my $post_data = '{ "name": "Dan", "address": "NY" }';
$req->content($post_data);

my $resp = $ua->request($req);
if ($resp->is_success) {
    my $message = $resp->decoded_content;
    print "Received reply: $message\n";
}
else {
    print "HTTP POST error code: ", $resp->code, "\n";
    print "HTTP POST error message: ", $resp->message, "\n";
}

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.


Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of Xmodulo.com. He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

2 thoughts on “How to send HTTP GET or POST request in Perl

  1. Hi Dan,

    Thank you for the useful post. How would the code look if we're using HTTP basic authentication instead. I am trying to leverage your code to make HTTP Get calls to a system to retrieve performance related metrics.

    Any guidance would be greatly appreciated.

    Thanks Julian

  2. Your concise and complete examples were super helpful. Just wanted to leave one remark for anyone else who runs into the issue I did:

    If one of the header keys you're trying to pass shouldn't be auto-capitalized or touched by the module, you can prepend it with a colon. For example:
    $req->header(":app_id" => $app_id);
    $req->header(":app_key" => $api_key);

    Thanks!

Leave a comment

Your email address will not be published. Required fields are marked *