0

New! Save questions or answers and organize your favorite content.
Learn more.

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 2

9

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

use LWP::UserAgent;
$ua = LWP::UserAgent->new;
$ua->head('<url>');
1
  • Lucky for me mechanize inherits from UserAgent :)
    – Geo
    Feb 7, 2009 at 14:03
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.