HTTP clients with HTTP::Tiny
A GET request
HTTP::Tiny is the HTTP client that ships with Perl. It has no dependencies for plain HTTP, handles redirects, and returns a small hash instead of an object graph.
use strict; use warnings; use HTTP::Tiny; use JSON::PP; my $ua = HTTP::Tiny->new( agent => "perl-course/1.0", timeout => 10, ); my $res = $ua->get("https://httpbin.org/json"); die "request failed: $res->{status} $res->{reason}\n" unless $res->{success}; my $data = JSON::PP->new->utf8->decode($res->{content}); print "keys: ", join(", ", sort keys %$data), "\n";
keys: slideshow
The response is a hash reference with status, reason, content, headers and a boolean success. Check success before reading content: an error page is still valid text, and decoding HTML as JSON produces a confusing message.
Pass a second hash reference to add request headers. The header names are case-insensitive, and values are joined with commas when repeated:
my $res = $ua->get($url, { headers => { Accept => "application/json", Authorization => "Bearer $token", }, });
POST and forms
post takes the same second argument plus a content key for the raw body. JSON APIs want an explicit content type, otherwise the server may reject or misread the payload:
my $body = JSON::PP->encode_json({ title => "Ship the course", done => JSON::PP::false, }); my $res = $ua->post("https://api.example.com/notes", { headers => { "Content-Type" => "application/json" }, content => $body, });
HTML forms use a different encoding: keys and values joined with ampersands and escaped. post_form does that encoding for you, so you do not have to remember the rules:
my $res = $ua->post_form("https://example.com/login", { user => "ada", pass => $secret, });
Every method has a generic form as well: $ua->request("DELETE", $url), or "PATCH" with a body. Use it for methods that have no named helper.
Reading the response
Status codes tell you what happened, and they are worth handling by class rather than one by one. A four-hundred series means the request was wrong; a five-hundred series means the server failed; three-hundred series are redirects that HTTP::Tiny follows for you.
if ($res->{status} == 429) { die "rate limited, retry later\n"; } elsif ($res->{status} >= 500) { die "server error: $res->{status} $res->{reason}\n"; }
Headers arrive lowercased, which saves you from guessing the server's capitalisation. Content type tells you how to parse the body:
my $type = $res->{headers}{"content-type"} // ""; if ($type =~ m{application/json}) { $data = JSON::PP->new->utf8->decode($res->{content}); }
When the request never reaches the server — a bad host name, a connection refused, a timeout — the status is 599 and the reason explains why. That is not an HTTP status, it is HTTP::Tiny's way of reporting a transport failure, so test for it explicitly.
Timeouts, redirects and TLS
The default timeout is sixty seconds, which is far too long for a script that talks to an API in a loop. Set a short timeout and decide what a failure means for your program.
HTTPS needs IO::Socket::SSL and Net::SSLeay. They are available from CPAN, and some distributions ship them by default. Certificate verification is on unless you turn it off, and you should not turn it off.
cpanm IO::Socket::SSL
my $ua = HTTP::Tiny->new( timeout => 5, max_redirect => 0, verify_SSL => 1, );
Setting max_redirect => 0 makes a redirect visible instead of following it, which is useful when you need the Location header. Proxies are read from http_proxy, https_proxy and no_proxy in the environment, so a script that works on your laptop also works behind the office proxy.
For cookies, multipart uploads or connection reuse, move to LWP::UserAgent or Mojo::UserAgent. HTTP::Tiny is deliberately small: it covers simple JSON and form APIs, and it covers them well.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a
getcall of HTTP::Tiny return?Answer
A hash reference with status, reason, content, headers and success
The small hash is the whole design of HTTP::Tiny: no object graph, just the fields a caller reads.
-
Which status does HTTP::Tiny report when the request never reaches the server?
Answer
599
599 is not an HTTP status; it is how the client reports a transport failure such as a bad host name or a timeout.
-
Why check
successbefore decodingcontentas JSON?Answer
An error page is still text, and decoding HTML as JSON gives a confusing message
The status tells you what kind of body arrived, and an error page is usually HTML rather than JSON.
-
What does
max_redirect => 0do?Answer
It turns a redirect into a visible response instead of following it
With redirects off the three-hundred response arrives as it is, and the Location header is yours to read.
-
What does
post_formdo thatpostdoes not?Answer
It encodes the keys and values as a form body
The form encoding joins the fields with ampersands and escapes them, so the caller passes a plain hash.
-
What does an HTTPS request need beyond HTTP::Tiny itself?
Answer
IO::Socket::SSL and Net::SSLeay
The TLS modules are on CPAN, and some distributions ship them, which is why HTTPS works from the same client.
-
What does this program print?
my $res = { status => 429, reason => "Too Many Requests", success => 0 }; if ($res->{status} == 429) { print "retry\n"; } elsif ($res->{status} >= 500) { print "server error\n"; }Answer
retry
The first branch matches the rate-limit status, so the later branch is never reached.
-
What does this program print?
my $res = { status => 599, reason => "Internal Exception", success => 0 }; print $res->{success} ? "ok\n" : "transport failed: $res->{reason}\n";Answer
transport failed: Internal Exception
The success field is false for a transport failure, and the reason names the cause.
-
What does this program print?
my $res = { headers => { "content-type" => "application/json; charset=utf-8" } }; my $type = $res->{headers}{"content-type"} // ""; print $type =~ m{application/json} ? "json\n" : "other\n";Answer
json
The lowercased header holds the media type, and the pattern matches the start of it.
-
What does this program print?
use HTTP::Tiny; my $ua = HTTP::Tiny->new(agent => "perl-course/1.0", timeout => 5); print $ua->{timeout}, " ", $ua->{agent}, "\n";Answer
5 perl-course/1.0
The constructor stores the options in the object, and the values replace the defaults.
Article author: Arthur Isaev