Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
HTTP::Tiny w/ gzip support
use utf8; use strict; use warnings; use feature qw'say state';
use URI; use HTTP::Tiny;
use IO::Uncompress::Gunzip 'gunzip';
say fetch('http://some-site');
sub fetch {
my $response = (state $ua = HTTP::Tiny->new(default_headers => {qw'Accept-Encoding gzip'}))->get(URI->new($_[0]));
say join '', ($response->{success} ? 'ok' : 'fail'), ': ', $_[0];
$response->{success} && length $response->{content}
? $response->{headers}{'content-encoding'} eq 'gzip'
? do { gunzip(\$response->{content}, \ my $buf); $buf }
: $response->{content}
: die "couldn't fetch\n" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment