Skip to content

Instantly share code, notes, and snippets.

@vividsnow
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vividsnow/2199b369b67dfcbd1749 to your computer and use it in GitHub Desktop.
Save vividsnow/2199b369b67dfcbd1749 to your computer and use it in GitHub Desktop.
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