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
2009-02-07 13:38:24
Lucky for me mechanize inherits from UserAgent :)
Geo
2009-02-07 14:03:29
+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
2009-02-07 21:40:21