Perl – Using HTTP::Lite

I recently had to use the HTTP::Lite perl module and thought I would share an example of how to use it. HTTP::Lite was is available on www.cpan.org; its very lightweight and has no dependancies other than Socket & Fcntl which included with almost all Perl implementations by default.
Based on the examples by the HTTP::Lite author Roy Hooper <[email protected]>
[perl]
#!perl
use HTTP::Lite;
#
# Get and print out the headers and body of the Google homepage
#
$http = new HTTP::Lite;
$req = $http->request(“http://www.google.co.uk/”)
or die “Unable to get document: $!”;
# Print the reconstructed status line
print $http->protocol() . ” ” . $req . ” ” . $http->status_message() . “”;
# Get the Headers & Body
@headers = $http->headers_array();
$body = $http->body();
# Display each Header
foreach $header (@headers) {
print $header . “\n”;
}
print “\n\n”;
# Display the Body
print $body . “\n”;
#
# Execute a Google Search using the POST method
#
$http = new HTTP::Lite;
# Post variables
%vars = (
“hl” => “en”,
“q” => “cpan”,
“btnG” => “Search”,
“meta” => “”
);
$http->prepare_post(\%vars);
$req = $http->request(“http://www.google.co.uk/search”)
or die “Unable to get document: $!”;
# Print the reconstructed status line
print $http->protocol() . ” ” . $req . ” ” . $http->status_message() . “”;
# Get the Headers & Body
@headers = $http->headers_array();
$body = $http->body();
# Display each Header
foreach $header (@headers) {
print $header . “\n”;
}
print “\n\n”;
# Display the Body
print $body . “\n”;
[/perl]
Google doesnt support queries by POST methods anymore, so the response to the second request here will be a “501 Not Implemented”.

Leave a Comment

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

Scroll to Top