Last active
August 29, 2015 14:19
-
-
Save vividsnow/2199b369b67dfcbd1749 to your computer and use it in GitHub Desktop.
HTTP::Tiny w/ gzip support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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