Search code examples
perlhttp-headerslwp

How to access HTTP headers from a given url?


I am looking for a way to get http headers from a given url. I tried the below code but it is not working:

#!/usr/bin/perl

#use warnings;
#use strict;
use LWP::UserAgent;

my $ua  = LWP::UserAgent->new;
my $res = $ua->get("http://cpan.org");
print $res->header('Content-Length');

The above code prints nothing. So, where am I going wrong here or is there any better way to access headers.


Solution

  • use LWP::UserAgent;
    
    my $ua  = LWP::UserAgent->new;
    my $res = $ua->get("http://cpan.org");
    print $res->headers->as_string;