Skip to content

Instantly share code, notes, and snippets.

@hernan604
Created October 21, 2013 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hernan604/7083667 to your computer and use it in GitHub Desktop.
Save hernan604/7083667 to your computer and use it in GitHub Desktop.
HTTP::Tiny request with gzip plus response decompress
use HTTP::Tiny;
use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ;
use Data::Printer;
my $ua = HTTP::Tiny->new(
agent => "Mozilla/5.0 (Windows 8; rv:24.0) Gecko/20100101 Firefox/24.0" ,
default_headers => {
'Accept-Encoding' => 'gzip'
}
) ;
sub _visit {
my ( $url ) = @_;
my $res = $ua->get( $url );
$res = _decompress($res);
return $res;
}
sub _decompress {
my ( $res ) = @_;
if (
exists $res->{ headers } &&
exists $res->{ headers }->{ 'content-encoding' } &&
$res->{ headers }->{ 'content-encoding' } eq 'gzip' ) {
warn "CONTENT COMPRESSED HERE... decompressing";
my $content = $res->{ content };
my ( $content_decompressed, $scalar, $GunzipError );
gunzip \$content => \$content_decompressed,
MultiStream => 1, Append => 1, TrailingData => \$scalar
or die "gunzip failed: $GunzipError\n";
$res->{ content } = $content_decompressed;
}
return $res;
}
my $res = _visit( 'http://www.uol.com.br' );
warn p $res;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment