HTTP::Tiny - A small, simple, correct HTTP/1.1 client
version 0.086
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};
This is a very simple HTTP/1.1 client, designed for doing simple requests without the overhead of a large framework like LWP::UserAgent.
It is more correct and more complete than HTTP::Lite. It supports proxies and redirection. It also correctly resumes after EINTR.
If IO::Socket::IP 0.25 or later is installed, HTTP::Tiny will use it instead of IO::Socket::INET for transparent support for both IPv4 and IPv6.
Cookie support requires HTTP::CookieJar or an equivalent class.
$http = HTTP::Tiny->new( %attributes );
This constructor returns a new HTTP::Tiny object. Valid attributes include:
agent
— A user-agent string (defaults to 'HTTP-Tiny/$VERSION'). If agent
— ends in a space character, the default user-agent string is appended.
cookie_jar
— An instance of HTTP::CookieJar — or equivalent class that supports the add
and cookie_header
methods
default_headers
— A hashref of default headers to apply to requests
local_address
— The local IP address to bind to
keep_alive
— Whether to reuse the last connection (if for the same scheme, host and port) (defaults to 1)
max_redirect
— Maximum number of redirects allowed (defaults to 5)
max_size
— Maximum response size in bytes (only when not using a data callback). If defined, requests with responses larger than this will return a 599 status code.
http_proxy
— URL of a proxy server to use for HTTP connections (default is $ENV{http_proxy}
— if set)
https_proxy
— URL of a proxy server to use for HTTPS connections (default is $ENV{https_proxy}
— if set)
proxy
— URL of a generic proxy server for both HTTP and HTTPS connections (default is $ENV{all_proxy}
— if set)
no_proxy
— List of domain suffixes that should not be proxied. Must be a comma-separated string or an array reference. (default is $ENV{no_proxy}
—)
timeout
— Request timeout in seconds (default is 60) If a socket open, read or write takes longer than the timeout, the request response status code will be 599.
verify_SSL
— A boolean that indicates whether to validate the TLS/SSL certificate of an https
— connection (default is true). Changed from false to true in version 0.083.
SSL_options
— A hashref of SSL_*
— options to pass through to IO::Socket::SSL
$ENV{PERL_HTTP_TINY_SSL_INSECURE_BY_DEFAULT}
- Changes the default certificate verification behavior to not check server identity if set to 1. Only effective if verify_SSL
is not set. Added in version 0.083.
An accessor/mutator method exists for each attribute.
Passing an explicit undef
for proxy
, http_proxy
or https_proxy
will prevent getting the corresponding proxies from the environment.
Errors during request execution will result in a pseudo-HTTP status code of 599 and a reason of "Internal Exception". The content field in the response will contain the text of the error.
The keep_alive
parameter enables a persistent connection, but only to a single destination scheme, host and port. If any connection-relevant attributes are modified via accessor, or if the process ID or thread ID change, the persistent connection will be dropped. If you want persistent connections across multiple destinations, use multiple HTTP::Tiny objects.
See