You are viewing the version of this documentation from Perl 5.14.4. View the latest version

CONTENTS

NAME

HTTP::Tiny - A small, simple, correct HTTP/1.1 client

VERSION

version 0.012

SYNOPSIS

use HTTP::Tiny;

my $response = HTTP::Tiny->new->get('http://example.com/');

die "Failed!\n" unless $response->{success};

print "$response->{status} $response->{reason}\n";

while (my ($k, $v) = each %{$response->{headers}}) {
    for (ref $v eq 'ARRAY' ? @$v : $v) {
        print "$k: $_\n";
    }
}

print $response->{content} if length $response->{content};

DESCRIPTION

This is a very simple HTTP/1.1 client, designed primarily for doing simple GET requests without the overhead of a large framework like LWP::UserAgent.

It is more correct and more complete than HTTP::Lite. It supports proxies (currently only non-authenticating ones) and redirection. It also correctly resumes after EINTR.

METHODS

new

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

This constructor returns a new HTTP::Tiny object. Valid attributes include:

get

$response = $http->get($url);
$response = $http->get($url, \%options);

Executes a GET request for the given URL. The URL must have unsafe characters escaped and international domain names encoded. Internally, it just calls request() with 'GET' as the method. See request() for valid options and a description of the response.

mirror

$response = $http->mirror($url, $file, \%options)
if ( $response->{success} ) {
    print "$file is up to date\n";
}

Executes a GET request for the URL and saves the response body to the file name provided. The URL must have unsafe characters escaped and international domain names encoded. If the file already exists, the request will includes an If-Modified-Since header with the modification timestamp of the file. You may specificy a different If-Modified-Since header yourself in the $options->{headers} hash.

The success field of the response will be true if the status code is 2XX or 304 (unmodified).

If the file was modified and the server response includes a properly formatted Last-Modified header, the file modification time will be updated accordingly.

request

$response = $http->request($method, $url);
$response = $http->request($method, $url, \%options);

Executes an HTTP request of the given method type ('GET', 'HEAD', 'POST', 'PUT', etc.) on the given URL. The URL must have unsafe characters escaped and international domain names encoded. A hashref of options may be appended to modify the request.

Valid options are:

If the content option is a code reference, it will be called iteratively to provide the content body of the request. It should return the empty string or undef when the iterator is exhausted.

If the data_callback option is provided, it will be called iteratively until the entire response body is received. The first argument will be a string containing a chunk of the response body, the second argument will be the in-progress response hash reference, as described below. (This allows customizing the action of the callback based on the status or headers received prior to the content body.)

The request method returns a hashref containing the response. The hashref will have the following keys:

On an exception during the execution of the request, the status field will contain 599, and the content field will contain the text of the exception.

LIMITATIONS

HTTP::Tiny is conditionally compliant with the HTTP/1.1 specification. It attempts to meet all "MUST" requirements of the specification, but does not implement all "SHOULD" requirements.

Some particular limitations of note include:

SEE ALSO

SUPPORT

Bugs / Feature Requests

Please report any bugs or feature requests by email to bug-http-tiny at rt.cpan.org, or through the web interface at http://rt.cpan.org/Public/Dist/Display.html?Name=HTTP-Tiny. You will be automatically notified of any progress on the request by the system.

Source Code

This is open source software. The code repository is available for public review and contribution under the terms of the license.

http://github.com/dagolden/p5-http-tiny/tree

git clone git://github.com/dagolden/p5-http-tiny.git

AUTHORS

COPYRIGHT AND LICENSE

This software is copyright (c) 2011 by Christian Hansen.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.