Geo
Geo

Reputation: 96907

How do I find a link's content type in Perl?

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?

Upvotes: 1

Views: 983

Answers (2)

brian d foy
brian d foy

Reputation: 132876

Here's a full example using LWP. If you use call the content_type in list context, you get the type and charset as separate values:

use v5.10;

my $url = 'https://www.perl.com';

use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $response = $ua->head( $url );
my( $type, $charset ) = $response->content_type;
say "LWP: Content type is ", $type;
say "LWP: charset is ", $charset;

Here's the same thing in Mojolicious, which doesn't break up the Content-type header for you.

use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
my $tx = $ua->head( $url );
my( $type, $charset ) =
    split /\s*;\s*/, $tx->result->headers->content_type, 2;
say "Mojo: Content type is ", $type;
say "Mojo: charset is ", $charset;

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.

Upvotes: 4

Rutesh Makhijani
Rutesh Makhijani

Reputation: 17235

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

use LWP::UserAgent;
$ua = LWP::UserAgent->new;
$ua->head('<url>');

Upvotes: 9

Related Questions