Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a gzip compressed HTTP::Response?

Tags:

perl

lwp

I need to create an HTTP::Response with compressed data. How do I go about making the content gzipped? Do I just add the appropriate headers and compress it myself using Compress::Zlib? Or does any of the LWP modules provide a method for handling this?

like image 449
mrburns Avatar asked Jun 22 '26 06:06

mrburns


1 Answers

Is this what you need? You gzip the data, set the Content-encoding header, and send it off.

use strict;
use warnings;

use HTTP::Response;
use IO::Compress::Gzip qw(gzip);

my $data = q(My cat's name is Buster);

my $gzipped_data;
my $gzip = gzip \$data => \$gzipped_data;
print STDERR $gzipped_data;

my $response = HTTP::Response->new;

$response->code( 200 );
$response->header( 'Content-type'     => 'text/plain' );
$response->header( 'Content-encoding' => 'gzip' );
$response->header( 'Content-length'   => length $gzipped_data );

$response->content( $gzipped_data );

print $response->as_string;
like image 125
brian d foy Avatar answered Jun 24 '26 23:06

brian d foy