Perl LWP::useragent capture server response headers
13,576
http://search.cpan.org/perldoc?HTTP::Response
use LWP::UserAgent;
my $ua = new LWP::UserAgent;
my $response = $ua->get("http://google.co.uk");
print $response->headers()->as_string;
print $response->header('content-type');
Author by
jippie
I'm into: Linux; Networking; IT Security; Perl; System integration; Arduino; ATtiny & ATmega Microcontrollers (AVR); Analog electronics;
Updated on June 05, 2022Comments
-
jippie almost 2 years
I'm querying a webserver for a document and I want to capture the both the document and the related server response headers (esp. Content-Type: ...). I have trouble finding out how to read the headers. Here are some sniplets from my Perl script, I left error checking out for clarity:
use LWP::UserAgent; my $ua = LWP::UserAgent->new; $ua->agent( 'requiredCustomUserAgent' ); # I'm required to set a custom user agent $imageData = $response->content; # This is the received document
So at this point I can retrieve the web document, but I want to know what Content-Type the server sent with it. Unfortunately this is not always the same as the mime type found by the bash 'file' command. This file-method fails in case of .js or .css documents.
-
jippie about 12 yearsI think the answer must be in search.cpan.org/~gaas/HTTP-Message-6.03/lib/HTTP/Response.pm and search.cpan.org/~gaas/HTTP-Message-6.03/lib/HTTP/Headers.pm but it seems I just don't know how to read the information on $r->header( $field => $value ) and $h->content_type
-
-
Jacques about 7 yearsJust a word of caution. Actually $response->headers->as_string would be expected to produce the raw response headers, but it simply does not. So one should be careful about this output. This is made particularly obvious when one looks at the Set-Cookie value.