views:

202

answers:

2

Suppose you're given an URL, http://site.com . How do you find out what it's content type is without downloading it? Can Perl's WWW::Mechanize or LWP issue a HEAD request?

+9  A: 

You can use head() method of LWP in following manner

use LWP::UserAgent;
$ua = LWP::UserAgent->new;
$ua->head('<url>');
Rutesh Makhijani
Lucky for me mechanize inherits from UserAgent :)
Geo
+3  A: 

Here's a full example:

use LWP::UserAgent;

$ua = LWP::UserAgent->new;
my $response = $ua->head( 'http://www.perl.com' );

my $type = $response->content_type;

print "The type is $type\n";

Some servers choke on HEAD requests, so when I do this and get an error of any sort, I retry it with a GET request and only request the first couple hundred of bytes of the resources.

brian d foy