Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl LWP::useragent capture server response headers

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.

like image 219
jippie Avatar asked Mar 15 '12 17:03

jippie


People also ask

What is LWP :: UserAgent?

The LWP::UserAgent is a class implementing a web user agent. LWP::UserAgent objects can be used to dispatch web requests. In normal use the application creates an LWP::UserAgent object, and then configures it with values for timeouts, proxies, name, etc.

What is LWP :: Simple?

LWP::SimpleReturns the contents of the URL specified by $url. Upon failure, get( ) returns undef. Other than returning undef, there is no way of accessing the HTTP status code or headers returned by the server. head($url)


1 Answers

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');
like image 58
aidan Avatar answered Oct 04 '22 20:10

aidan