Showing the request's and the response's headers
The following example shows the headers that were sent with the request and the headers that the user agent received from the web server:
#!/usr/bin/perl
use warnings;
use strict;
use LWP::UserAgent;
my $user_agent = LWP::UserAgent->new;
my $request = HTTP::Request->new('GET' => 'http://renenyffenegger.ch/robots.txt');
my $response = $user_agent->request($request);
print "Request Headers\n";
show_headers($response->request);
print "Response Headers\n";
show_headers($response);
print $response->content;
sub show_headers {
my $request = shift;
my $headers = $request->headers;
for my $header_field_name($headers->header_field_names) {
printf " %-50s: %s\n", $header_field_name, $headers->header($header_field_name) // '?';
}
}
$response->decoded_content
A user agent can use the HTTP header Accept-encoding to indicate that it is prepared to receive compressed data from the server.
If the server then chooses to send its data gzip'ed (which it is not required to do), it will send the header Content-Encoding with the value of the algorithm used for compression.
The following example passes gzip
with that header and checks whether the server has actually set the Content-Encoding header.
If it did, the response will be taken from $response->decoded_content
which contains the uncompressed data.
#!/usr/bin/perl
use warnings;
use strict;
use LWP::UserAgent;
my $user_agent = LWP::UserAgent->new;
my $request = HTTP::Request->new('GET' => 'https://stackoverflow.com');
$request -> header('Accept-encoding', 'gzip');
my $response = $user_agent->request($request);
if ($response->headers->header('Content-Encoding') eq 'gzip') {
print "---------------------------------------------\n";
print "Server sent gzip encoded content, decoding...\n";
print "---------------------------------------------\n";
print substr($response->decoded_content, 0, 500);
}
else {
print "---------------------------------------\n";
print "Server didn't send gzip encoded content\n";
print "---------------------------------------\n";
print substr($response->content, 0, 500);
}