HTTP::Tiny(3pm)
π
HTTP::Tiny(3pm) β A small, simple, correct HTTP/1.1 client β¨
okay so bestie, welcome to HTTP::Tiny(3pm) from the Perl Programmers Reference Guide. This is the tiny HTTP client that's here to make simple requests feel effortless, and we're gonna read it together like a group chat. π
πΈ NAME πΈ
HTTP::Tiny β a small, simple, correct HTTP/1.1 client. That's the whole name, periodt. π
π VERSION π
version 0.096. That's the version, bestie. β¨
π¦ SYNOPSIS π¦
Here's the vibe check in code form, fr fr:
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 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. iconic behavior. π
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. π
π«Ά METHODS π«Ά
Okay bestie, this is the main character section. π
β¨ 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"). Ifagentends in a space character, the default user-agent string is appended.allow_credentialed_redirectsβ If a3XXredirects to a different scheme, host or port, by defaultHTTP::Tinywill strip away caller-suppliedAuthorization,CookieandProxy-Authorizationheaders from the redirected request and from all subsequent requests in the chain. Set this to a true value to revert to the legacy behavior of forwarding those headers. Default isfalse.allow_downgradeβ If a3XXredirect changes the scheme fromhttpsto plainhttp,HTTP::Tinywill by default refuse to follow it, returning the3XXresponse. Set this to a true value to revert to the legacy behavior of redirectinghttpstohttp. Default isfalse.cookie_jarβ An instance ofHTTP::CookieJarβ or equivalent class that supports theaddandcookie_headermethodsdefault_headersβ A hashref of default headers to apply to requestslocal_addressβ The local IP address to bind tokeep_aliveβ Whether to reuse the last connection (if for the same scheme, host and port) (defaults to1)keep_alive_timeoutβ How many seconds to keep a connection available for after a request (defaults to0, unlimited)max_redirectβ Maximum number of redirects allowed (defaults to5)max_sizeβ Maximum response size in bytes (only when not using a data callback). If defined, requests with responses larger than this will return a599status 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 is60). If a socket open, read or write takes longer than the timeout, the request response status code will be599.verify_SSLβ A boolean that indicates whether to validate the TLS/SSL certificate of anhttpsconnection (default is true). Changed from false to true in version0.083.SSL_optionsβ A hashref ofSSL_*β options to pass through toIO::Socket::SSL$ENV{PERL_HTTP_TINY_SSL_INSECURE_BY_DEFAULT}β Changes the default certificate verification behavior to not check server identity if set to1. Only effective ifverify_SSLis not set. Added in version0.083.
An accessor/mutator method exists for each attribute. iykyk. π
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.
The keep_alive_timeout parameter allows you to control how long a keep alive connection will be considered for reuse. By setting this lower than the server keep alive time, this allows you to avoid race conditions where the server closes the connection while preparing to write the request on a reused persistent connection.
See TLS/SSL SUPPORT for more on the verify_SSL and SSL_options attributes.
π¦ get|head|put|post|patch|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. If data is provided as an array reference, the order is preserved; if provided as a hash reference, the terms are sorted by key for consistency. 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 include 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.
NOTE: Method names are case-sensitive per the HTTP/1.1 specification. Don't use get when you really want GET. See LIMITATIONS for how this applies to redirection.
If the URL includes a user:password stanza, they will be used for Basic-style authorization headers. For example:
$http->request('GET', 'http://Aladdin:open sesame@example.com/');
If the user:password stanza contains reserved characters, they must be percent-escaped:
$http->request('GET', 'http://john%40example.com:password@example.com/');
Caller-supplied Authorization, Cookie and Proxy-Authorization headers are stripped on cross-origin redirects. See new's allow_credentialed_redirects attribute to opt out.
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 requesttrailer_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 chunk of the response body received.peerβ Override host resolution and force all connections to go only to a specific peer address, regardless of the URL of the request. This will include any redirections! This options should be used with extreme caution (e.g. debugging or very special circumstances). It can be given as either a scalar or a code reference that will receive the hostname and whose response will be taken as the address.
The Host header is generated from the URL in accordance with RFC 2616. It is a fatal error to specify Host in the headers option. Other headers may be ignored or overwritten if necessary for transport compliance.
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 content option is the empty string, no content-type or content-length headers will be generated.
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.)
like, real talk, the content data in the request/response is handled as raw bytes π β any encoding/decoding (and the headers that go with it) is your job, not ours. the caller's. you. it's you.
The request method hands back a hashref with the response in it, and that hashref has these keys, bestie:
successβ a boolean that says whether the operation came back with a2XXstatus code πurlβ the URL that provided the response. this is the request URL unless there were redirections, in which case it's the last URL queried in the redirect chain πstatusβ the HTTP status code of the responsereasonβ the response phrase the server gave backcontentβ the body of the response. if the response has no content, or if a data callback was provided to consume the response body, this is the empty string π«headersβ a hashref of header fields. every header field name is normalized to lowercase. if a header is repeated, the value is an arrayref; otherwise it's a scalar string holding the value πprotocolβ if this field exists, it's the protocol of the response, likeHTTP/1.0orHTTP/1.1redirectsβ if this field exists, it's an arrayref of response hashrefs from redirects, in the same order the redirections happened. if it doesn't exist, then no redirections happened β¨
if something goes wrong while the request is executing, thestatusfield will contain599, and thecontentfield will contain the text of the error π like, that's how you know it went sideways.
π¦ 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 an x-www-form-urlencoded string. the keys and values from the data reference get UTF-8 encoded and escaped per RFC 3986 π§ if a value is an array reference, the key gets repeated with each of the values of that array reference. and if data is given as a hash reference, the key/value pairs in the resulting string get sorted by key and value so the ordering is consistent, periodt π
β can_ssl β
$ok = HTTP::Tiny->can_ssl;
($ok, $why) = HTTP::Tiny->can_ssl;
($ok, $why) = $http->can_ssl;
Tells you if SSL support is available. when it's called as a class object, it checks for the right version of Net::SSLeay and IO::Socket::SSL π when it's called as an object method, if SSL_verify is true or if SSL_verify_mode is set in SSL_options, it checks that a CA file is available.
In scalar context it returns a boolean saying whether SSL is available. in list context it returns that boolean plus a (possibly multi-line) string of errors explaining why SSL isn't available π very "here's the receipt" of it all.
πΈ connected πΈ
$host = $http->connected;
($host, $port) = $http->connected;
Tells you if a connection to a peer is being kept alive, per the keep_alive option π
In scalar context, returns the peer host and port joined with a colon, or undef if no peer is connected. in list context, returns the peer host and port, or an empty list if no peer is connected.
heads up: this method cannot reliably be used to discover whether the remote host has closed its end of the socket π like it's just not gonna tell you that, bestie.
π TLS/SSL SUPPORT π
Direct https connections are only supported if IO::Socket::SSL 1.56 or greater and Net::SSLeay 1.49 or greater are installed. an error will occur if new enough versions of these modules aren't installed, or if the TLS encryption fails π¬ You can also use the HTTP::Tiny::can_ssl() utility function, which returns a boolean so you can see if the required modules are installed.
An https connection may be made via an http proxy that supports the CONNECT command (i.e. RFC 2817). but you may not proxy https via a proxy that itself requires https to communicate π
ββοΈ
TLS/SSL gives you two distinct capabilities, and they're both kind of iconic:
- Encrypted communication channel π
- Verification of server identity πͺͺ
By default, HTTP::Tiny verifies server identity.
This was changed in version 0.083 because of security concerns. the previous default behavior can be enabled by setting $ENV{PERL_HTTP_TINY_SSL_INSECURE_BY_DEFAULT} to 1 β οΈ (yes that env var name is screaming at you, it's supposed to)
Verification is done by checking that the TLS/SSL connection has a valid certificate corresponding to the host name of the connection, and that the certificate has been verified by a CA. assuming you trust the CA, this protects against machine-in-the-middle attacks <http://en.wikipedia.org/wiki/Machine-in-the-middle_attack> π‘οΈ
Certificate verification requires a file or directory containing trusted CA certificates.
IO::Socket::SSL::default_ca() is called to detect the default location of your CA certificates. this also supports the environment variables SSL_CERT_FILE and SSL_CERT_DIR, and will fail over to Mozilla::CA if no certs are found π
If IO::Socket::SSL::default_ca() isn't able to find usable CA certificates, HTTP::Tiny will search several well-known system-specific default locations for a CA certificate file as a last resort:
/etc/ssl/certs/ca-certificates.crt/etc/pki/tls/certs/ca-bundle.crt/etc/ssl/ca-bundle.pem/etc/openssl/certs/ca-certificates.crt/etc/ssl/cert.pem/usr/local/share/certs/ca-root-nss.crt/etc/pki/tls/cacert.pem/etc/certs/ca-certificates.crt
An error will occur if verify_SSL is true and no CA certificate file is available π€
If you want complete control over TLS/SSL connections, the SSL_options attribute lets you provide a hash reference that gets passed straight through to IO::Socket::SSL::start_SSL(), overriding any options set by HTTP::Tiny. for example, to provide your own trusted CA file:
SSL_options => {
SSL_ca_file => $file_path,
}
The SSL_options attribute can also be used for things like providing a client certificate for authentication to a server, or controlling the choice of cipher used for the TLS/SSL connection ποΈ See the IO::Socket::SSL documentation for details.
π PROXY SUPPORT π
HTTP::Tiny can proxy both http and https requests. only Basic proxy authorization is supported, and it has to be provided as part of the proxy URL: http://user:pass@proxy.example.com/ π
HTTP::Tiny supports these proxy environment variables:
http_proxyorHTTP_PROXYhttps_proxyorHTTPS_PROXYall_proxyorALL_PROXY
If the REQUEST_METHOD environment variable is set, then this might be a CGI process and HTTP_PROXY would be set from the Proxy: header, which is a security risk β οΈ so if REQUEST_METHOD is set, HTTP_PROXY (the uppercase variant only) is ignored, but CGI_HTTP_PROXY is considered instead. iykyk π
Tunnelling https over an http proxy using the CONNECT method is supported. if your proxy uses https itself, you can not tunnel https over it π«
be warned that proxying an https connection opens you to the risk of a man-in-the-middle attack by the proxy server π like, the proxy can see everything. that's the whole deal.
The no_proxy environment variable is supported, in the format of a comma-separated list of domain extensions the proxy should not be used for.
Proxy arguments passed to new will override their corresponding environment variables π
π LIMITATIONS π
HTTP::Tiny is conditionally compliant with the HTTP/1.1 specifications <http://www.w3.org/Protocols/>:
- "Message Syntax and Routing" [RFC7230]
- "Semantics and Content" [RFC7231]
- "Conditional Requests" [RFC7232]
- "Range Requests" [RFC7233]
- "Caching" [RFC7234]
- "Authentication" [RFC7235]
It attempts to meet all the MUST requirements of the spec, but doesn't implement all the SHOULD requirements. (Note: it was developed against the earlier RFC 2616 specification and may not yet meet the revised RFC 7230-7235 spec.) additionally, HTTP::Tiny supports the PATCH method of RFC 5789 π©Ή
Some particular limitations of note include:
- HTTP::Tiny focuses on correct transport. users are responsible for making sure user-defined headers and content are compliant with the HTTP/1.1 specification π§
- Users must make sure 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,307and308if the request method is'GET'or'HEAD'. response code303is always converted into a'GET'redirection, as mandated by the specification. there's no automatic support for status305("Use proxy") redirections π - There's no provision for delaying a request body using an
Expectheader. unexpected1XXresponses are silently ignored as per the specification π€ - Only 'chunked'
Transfer-Encodingis supported. - There's no support for a Request-URI of
'*'for the'OPTIONS'request. - Headers mentioned in the RFCs and some other well-known headers are generated with their canonical case. other headers are sent in the case provided by the user. except for control headers (which are sent first), headers are sent in arbitrary order π
Despite all the limitations listed above, HTTP::Tiny is considered feature-complete β¨ new feature requests should be directed to HTTP::Tiny::UA.
π«Ά SEE ALSO π«Ά
- HTTP::Tiny::UA β higher level UA features for HTTP::Tiny
- HTTP::Thin β HTTP::Tiny wrapper with HTTP::Request/HTTP::Response compatibility
- HTTP::Tiny::Mech β wrap a WWW::Mechanize instance in an HTTP::Tiny compatible interface
- IO::Socket::IP β required for IPv6 support
- IO::Socket::SSL β required for SSL support
- LWP::UserAgent β if HTTP::Tiny isn't enough for you, this is the "standard" way to do things
- Net::SSLeay β required for SSL support
π§ SUPPORT π§
π Bugs / Feature Requests π
Please report any bugs or feature requests through the issue tracker at <https://github.com/Perl-Toolchain-Gang/HTTP-Tiny/issues>. you'll 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/Perl-Toolchain-Gang/HTTP-Tiny>
git clone https://github.com/Perl-Toolchain-Gang/HTTP-Tiny.git
π AUTHORS π
- Christian Hansen <chansen@cpan.org>
- David Golden <dagolden@cpan.org>
π CONTRIBUTORS π
okay so this page has a lot of names on it and honestly every single one of them ate. these are the people who sent patches, fixed bugs, argued about proxy behaviour in the group chat (the issue tracker), and generally made this little HTTP client what it is. no cap, show them some respect π«Ά
- Alan Gardner β
gardner@pythian.com - Alessandro Ghedini β
al3xbio@gmail.com - A. Sinan Unur β
nanis@cpan.org - Brad Gilbert β
bgills@cpan.org - brian m. carlson β
sandals@crustytoothpaste.net - Chris Nehren β
apeiron@cpan.org - Chris Weyl β
cweyl@alumni.drew.edu - Claes Jakobsson β
claes@surfar.nu - Clinton Gormley β
clint@traveljury.com - Craig A. Berry β
craigberry@mac.com - Dan β
grinnz@gmail.com - David Golden β
xdg@xdg.me - David Mitchell β
davem@iabyn.com - Dean Pearce β
pearce@pythian.com - Edward Zborowski β
ed@rubensteintech.com - Felipe Gasper β
felipe@felipegasper.com - Graham Knop β
haarg@haarg.org - Greg Kennedy β
kennedy.greg@gmail.com - James E Keenan β
jkeenan@cpan.org - James Raspass β
jraspass@gmail.com - Jeremy Mates β
jmates@cpan.org - Jess Robinson β
castaway@desert-island.me.uk - Karen Etheridge β
ether@cpan.org - Lukas Eklund β
leklund@gmail.com - Martin J. Evans β
mjegh@ntlworld.com - Martin-Louis Bright β
mlbright@gmail.com - Matthew Horsfall β
wolfsage@gmail.com - Michael R. Davis β
mrdvt92@users.noreply.github.com - Michael Stevens β
michael.stevens@dianomi.com - Mike Doherty β
doherty@cpan.org - Nicolas Rochelemagne β
rochelemagne@cpanel.net - Olaf Alders β
olaf@wundersolutions.com - Olivier MenguΓ© β
dolmen@cpan.org - Petr PΓsaΕ β
ppisar@redhat.com - Philippe Bruhat (BooK) β
book@cpan.org - Rob Mueller β
robm@fastmailteam.com - sanjay-cpu β
snjkmr32@gmail.com - Serguei Trouchelle β
stro@cpan.org - Shoichi Kaji β
skaji@cpan.org - SkyMarshal β
skymarshal1729@gmail.com - SΓΆren Kornetzki β
soeren.kornetzki@delti.com - Steve Grazzini β
steve.grazzini@grantstreet.com - Stig Palmquist β
git@stig.io - Syohei YOSHIDA β
syohex@gmail.com - Tatsuhiko Miyagawa β
miyagawa@bulknews.net - Tom Hukins β
tom@eborcom.com - Tony Cook β
tony@develop-help.com - Xavier Guimard β
yadd@debian.org
likeβ¦ forty-eight whole legends. the way that list just keeps going?? iconic. if your name is on here, bestie, you're literally part of Unix history, go tell your mom π
π COPYRIGHT AND LICENSE π
the legal bit, but make it cute (it is what it is, we don't argue with lawyers):
This software is copyright (c) 2026 by Christian Hansen.
and the freedom part β pay attention, this is genuinely good news:
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
so yeah, you can copy it, remix it, ship it, build your weird little project on top of it β just keep the same terms as Perl 5 itself and we're all good π§β¨
and that's the whole page, bestie!! you made it to the end π this was source perl v5.42.3, dated 2026-11-03, and it's the HTTP::Tiny(3pm) page β now go make some requests, set a timeout, remember you have to call ->get, ->post etc. (not ->request directly, that one's internal π), and slay π
π¦
byeeee, go touch some sockets π
π¦ where this page comes from
- alpine 3.24: perl-doc 5.42.3-r0 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms