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?
2 Answers
You can use head() method of LWP in following manner
use LWP::UserAgent;
$ua = LWP::UserAgent->new;
$ua->head('<url>');
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.