table of contents
other versions
- wheezy 0.022-1
- wheezy-backports 0.043-2~bpo70+1
- jessie 0.050-1
- testing 0.070-1
- unstable 0.070-1
other sections
| HTTP::Tiny(3pm) | User Contributed Perl Documentation | HTTP::Tiny(3pm) |
NAME¶
HTTP::Tiny - A small, simple, correct HTTP/1.1 clientVERSION¶
version 0.022SYNOPSIS¶
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 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:
- •
- "agent"
A user-agent string (defaults to 'HTTP::Tiny/$VERSION')
- •
- "default_headers"
A hashref of default headers to apply to requests
- •
- "local_address"
The local IP address to bind to
- •
- "max_redirect"
Maximum number of redirects allowed (defaults to 5)
- •
- "max_size"
Maximum response size (only when not using a data callback). If defined, responses larger than this will return an exception.
- •
- "proxy"
URL of a proxy server to use (default is $ENV{http_proxy} if set)
- •
- "timeout"
Request timeout in seconds (default is 60)
- •
- "verify_SSL"
A boolean that indicates whether to validate the SSL certificate of an "https" connection (default is false)
- •
- "SSL_options"
A hashref of "SSL_*" options to pass through to IO::Socket::SSL
get|head|put|post|delete¶
$response = $http->get($url);
$response = $http->get($url, \%options);
$response = $http->head($url);
These methods are shorthand for calling "request()" for the given
method. The URL must have unsafe characters escaped and international domain
names encoded. See "request()" for valid options and a description
of the response.
The "success" field of the response will be true if the status code is
2XX.
post_form¶
$response = $http->post_form($url, $form_data);
$response = $http->post_form($url, $form_data, \%options);
This method executes a "POST" request and sends the key/value pairs
from a form data hash or array reference to the given URL with a
"content-type" of "application/x-www-form-urlencoded". See
documentation for the "www_form_urlencode" method for details on the
encoding.
The URL must have unsafe characters escaped and international domain names
encoded. See "request()" for valid options and a description of the
response. Any "content-type" header or content in the options
hashref will be ignored.
The "success" field of the response will be true if the status code is
2XX.
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 specify 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 if the status code is 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:
- •
- "headers"
A hashref containing headers to include with the request. If the value for a header is an array reference, the header will be output multiple times with each value in the array. These headers over-write any default headers.
- •
- "content"
A scalar to include as the body of the request OR a code reference that will be called iteratively to produce the body of the request
- •
- "trailer_callback"
A code reference that will be called if it exists to provide a hashref of trailing headers (only used with chunked transfer-encoding)
- •
- "data_callback"
A code reference that will be called for each chunks of the response body received.
- •
- "success"
Boolean indicating whether the operation returned a 2XX status code
- •
- "url"
URL that provided the response. This is the URL of the request unless there were redirections, in which case it is the last URL queried in a redirection chain
- •
- "status"
The HTTP status code of the response
- •
- "reason"
The response phrase returned by the server
- •
- "content"
The body of the response. If the response does not have any content or if a data callback is provided to consume the response body, this will be the empty string
- •
- "headers"
A hashref of header fields. All header field names will be normalized to be lower case. If a header is repeated, the value will be an arrayref; it will otherwise be a scalar string containing the value
www_form_urlencode¶
$params = $http->www_form_urlencode( $data );
$response = $http->get("http://example.com/query?$params");
This method converts the key/value pairs from a data hash or array reference
into a "x-www-form-urlencoded" string. The keys and values from the
data reference will be UTF-8 encoded and escaped per RFC 3986. If a value is
an array reference, the key will be repeated with each of the values of the
array reference. The key/value pairs in the resulting string will be sorted by
key and value.
SSL SUPPORT¶
Direct "https" connections are supported only if IO::Socket::SSL 1.56 or greater is installed. An exception will be thrown if a new enough IO::Socket::SSL is not installed or if the SSL encryption fails. There is no support for "https" connections via proxy (i.e. RFC 2817). SSL provides two distinct capabilities:- •
- Encrypted communication channel
- •
- Verification of server identity
- •
- /etc/ssl/certs/ca-certificates.crt
- •
- /etc/pki/tls/certs/ca-bundle.crt
- •
- /etc/ssl/ca-bundle.pem
SSL_options => {
SSL_ca_file => $file_path,
}
The "SSL_options" attribute could also be used for such things as
providing a client certificate for authentication to a server or controlling
the choice of cipher used for the SSL connection. See IO::Socket::SSL
documentation for details.
LIMITATIONS¶
HTTP::Tiny is conditionally compliant with the HTTP/1.1 specification <http://www.w3.org/Protocols/rfc2616/rfc2616.html>. It attempts to meet all "MUST" requirements of the specification, but does not implement all "SHOULD" requirements. Some particular limitations of note include:- •
- HTTP::Tiny focuses on correct transport. Users are responsible for ensuring that user-defined headers and content are compliant with the HTTP/1.1 specification.
- •
- Users must ensure that URLs are properly escaped for unsafe characters and that international domain names are properly encoded to ASCII. See URI::Escape, URI::_punycode and Net::IDN::Encode.
- •
- Redirection is very strict against the specification. Redirection is only automatic for response codes 301, 302 and 307 if the request method is 'GET' or 'HEAD'. Response code 303 is always converted into a 'GET' redirection, as mandated by the specification. There is no automatic support for status 305 ("Use proxy") redirections.
- •
- Persistent connections are not supported. The "Connection" header will always be set to "close".
- •
- Cookies are not directly supported. Users that set a "Cookie" header should also set "max_redirect" to zero to ensure cookies are not inappropriately re-transmitted.
- •
- Only the "http_proxy" environment variable is supported in the format "http://HOST:PORT/". If a "proxy" argument is passed to "new" (including undef), then the "http_proxy" environment variable is ignored.
- •
- There is no provision for delaying a request body using an "Expect" header. Unexpected "1XX" responses are silently ignored as per the specification.
- •
- Only 'chunked' "Transfer-Encoding" is supported.
- •
- There is no support for a Request-URI of '*' for the 'OPTIONS' request.
- •
- There is no support for IPv6 of any kind.
SEE ALSO¶
- •
- LWP::UserAgent
- •
- IO::Socket::SSL
- •
- Mozilla::CA
SUPPORT¶
Bugs / Feature Requests¶
Please report any bugs or feature requests through the issue tracker at http://rt.cpan.org/Public/Dist/Display.html?Name=HTTP-Tiny <http://rt.cpan.org/Public/Dist/Display.html?Name=HTTP-Tiny>. You will be notified automatically of any progress on your issue.Source Code¶
This is open source software. The code repository is available for public review and contribution under the terms of the license. https://github.com/dagolden/p5-http-tiny <https://github.com/dagolden/p5-http-tiny>git clone https://github.com/dagolden/p5-http-tiny.git
AUTHORS¶
- •
- Christian Hansen <chansen@cpan.org>
- •
- David Golden <dagolden@cpan.org>
- •
- Mike Doherty <doherty@cpan.org>
COPYRIGHT AND LICENSE¶
This software is copyright (c) 2012 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.| 2012-06-02 | perl v5.14.2 |