| Subcribe via RSS

Perl Module Monday: HTTP::Tiny

October 17th, 2011 Posted in CPAN, HTTP, Perl

I’m still deep in the Stanford AI class, so this will be a light-weight posting. And since it’s going to be light-weight anyway, I’ll cover a module in the *::Tiny namespace: HTTP::Tiny.

HTTP::Tiny is a simple HTTP/1.1 client library with plenty of options. It handles HTTPS (if you have IO::Socket::SSL available) as well as HTTP requests, and does all the basic HTTP verbs. As is the case with most *::Tiny modules, the goal is to do as much as one can, without the overhead or dependency chain of a larger module. In this case HTTP::Tiny stands as a replacement for LWP::UserAgent, for those cases when you don’t need the full functionality that LWP provides.

The main methods of HTTP::Tiny that you’re likely to utilize (besides the constructor) are request() and get() (which is just a front-end to request(), with the ‘method’ argument set to GET). There is also a method called mirror(), which is handy for making a local copy of a web resource on your filesystem. mirror() even sets an “If-Modified-Since” header on the request, if the file already exists. A nice touch to have added! The request() method allows for a very useful range of options, that make it easy to pass specific headers, use call-back subroutines for either (or both) of the request body or the processing of the response, and provide trailer headers for chunked transfer-encoding. One thing I find curious, though, is why the author provides a short-hand method for the GET request, but not for the other verbs. Since all are called using the same semantics, it seems to me like it would have made as much sense to provide head(), put(), etc.

Still, it’s a nice little approach to HTTP communication, that doesn’t require as much setting-up of resources as LWP generally does. It doesn’t have the flexibility that LWP does, either, but sometimes you just don’t need that. You just need to get going in a few lines:

use HTTP::Tiny;

my $http = HTTP::Tiny->new();

for my $url (@ARGV)
    (my $file = $url) =~ s{^.*/}{};
    if (! $file)
        warn "Skipping $url (no file component)\n";
    $http->mirror($url, $file);

The above just mirrors all the URLs passed in via @ARGV, using the last file element of the URL as the file name to save to. It doesn’t have the progress-bar and summary that LWP’s “lwp-download” has, but it gets the job done.

So have a look, this could be a useful addition to your toolkit, sitting beside LWP and handling some of the simpler tasks for it.

2 Responses to “Perl Module Monday: HTTP::Tiny”

  1. Mark DominusNo Gravatar Says:

    Can you compare it with LWP::Simple?

  2. David GoldenNo Gravatar Says:

    The major difference is that HTTP::Tiny is under 700 lines of code and has no non-core dependencies, whereas LWP::Simple requires LWP::UserAgent and all of its dependencies. That made HTTP::Tiny suitable for inclusion with core Perl (as of v5.14) so that CPAN can be bootstrapped over HTTP using pure Perl.

    In functionality, HTTP::Tiny gives you some slight control over the user agent attributes whereas LWP::Simple gives you only the defaults of LWP::UserAgent. There are probably some minor differences in redirect handling — e.g. HTTP::Tiny will redirect POST requests if and only if the response is 303 (and it will convert to a GET) as per the RFC.

    As to why there aren’t helpers for other methods, it’s mostly because of the “Tiny” philosophy that says that only the most essential functions should be included.

Leave a Reply