steve2k2
steve2k2

Reputation: 11

Can I decode gzip when using the mirror function in LWP Useragent?

Can I use something like $response->decoded_content within a LWP UserAgent 'mirror' request? Thank you.

Upvotes: 1

Views: 174

Answers (1)

Håkon Hægland
Håkon Hægland

Reputation: 40718

When using mirror() the received data is not added to the response object directly, but instead written directly to the mirror file. This means that decoded_content() will not work. However, you can add a response_header that enables the storage of the received data:

use strict;
use warnings;
use LWP::UserAgent ();

my $ua = LWP::UserAgent->new;

my $fn = 'libwww-perl-6.41.tar.gz';  # Example file..
my $url = 'https://cpan.metacpan.org/authors/id/O/OA/OALDERS/'. $fn;
$ua->add_handler(
    response_header => sub {
        my($response, $ua, $handler) = @_;
        $response->{default_add_content} = 1;
    }
);
my $response = $ua->mirror($url, $fn);
if ( $response->is_success ) {
    if ( $response->header('Content-Type') eq 'application/x-gzip') {
        $response->header('Content-Encoding' => 'gzip');
    }
    my $decoded_content = $response->decoded_content;
    # Do someting with the decoded content here ...
}

Upvotes: 2

Related Questions