Search notes:

Perl module LWP::UserAgent

use warnings;
use strict;

use LWP::UserAgent;

my $proxy_user     = 'foo';
my $proxy_password = 'bar';

# Setting agent to prevent «406 Not Acceptable» that some servers throw.
my $ua = LWP::UserAgent->new(agent => "The agent that never sleeps");

# $ua -> proxy('http' , "http://$proxy_user:$proxy_password\@proxy.server.foo:8080");
# $ua -> proxy('https', "http://$proxy_user:$proxy_password\@proxy.server.foo:8080");

# my $response = $ua -> request(HTTP::Request->new(GET=>'http://www.adp-gmbh.ch/index.html'));
my $response = $ua -> request(HTTP::Request->new(GET=>'https://raw.githubusercontent.com/ReneNyffenegger/PerlModules/master/LWP/UserAgent/script.pl'));

print $response->content;
Github repository PerlModules, path: /LWP/UserAgent/script.pl

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) // '?';
  }

}
Github repository PerlModules, path: /LWP/UserAgent/show-headers.pl

$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);
}
Github repository PerlModules, path: /LWP/UserAgent/decoded_content.pl

See also

Perl modules
The get function of LWP::UserAgent returns a HTTP::Response object.
LWP::UserAgent needs (method request()) or creates (methods get(), post() etc) a HTTP::Request object to process a HTTP request.
HTTP::Async can be used to download resources in parallel rather than sequential.

Index