How to make a HTTP get request in Perl

Last updated on April 19th, 2022 at 10:43 am

There are many methods in perl on how to send HTTP GET request and grab it on the targeted web page. Different modules are available which will allow us to get the requested variables easily. Here i am not going use any modules. Just a simple straight forward script for displaying or saving the HTTP GET request variable which was submitted from a HTML form.

If you are using modules then LWP::UserAgent is the best one. Check this tutorial on how to install perl modules easily in Linux.(without using CPAN)

Here is the HTML form script

<form name="server" action="grab.pl" method="GET">
Enter Your Name <input type="text" name="myname">
<input type="submit" value="Sumit">

This will be the perl script that grabs the name that you have entered on the form

grab.pl will look like this

#!/usr/local/bin/perl -w
use CGI qw(:standard);
print header;
my $buffer;
my $pair;
my $name;
my $value;
my $FORM;
my %FORM;
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
    if ($ENV{'REQUEST_METHOD'} eq "GET")
    {
        $buffer = $ENV{'QUERY_STRING'};
}
    # Split information into name/value pairs
    my @pairs = split(/=/, $buffer);
#ends
my $name= $pairs[1];
print "My Name Is $name";

Please make sure you have changed the path of the interpreter / Shebang (/usr/local/bin/perl) in the script above.

Sample Output (Not using the HTML form and directly passing the parameter in the script

# curl localhost/cgi-bin/grab.pl?myname=thomas
My Name Is thomas
#
Click to rate this tutorial!
[Total: 0 Average: 0]

Leave a Reply

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