For starters, head does a HEAD request (as opposed to a GET or a POST request), so it's useless to call both get and head.

Then, if you read the documentation, you'll see that head returns five values in array context. That doesn't help you. However, in scalar context, head returns an HTTP::Response object, and that contains the headers of the response.

HTTP::Response inherits headers from HTTP::Message, which returns an HTTP::Headers object. HTTP::Headers has a as_string method that returns "the header fields as a formatted MIME header. Since it internally uses the scan method to build the string, the result will use case as suggested by HTTP spec, and it will follow recommended 'Good Practice' of ordering the header fields."

So,

use strict; use warnings; use LWP::Simple; my $url = "http://www.cpan.org/"; my $response = head($url) or die("Error fetching URL $url\n"); print($response->protocol(), ' '); print($response->status_line(), "\n"); print($response->headers_as_string()); # $response->headers->as_string

Variation 1: The same, for for GET or POST

use strict; use warnings; use LWP::UserAgent; my $url = "http://www.cpan.org/"; my $ua = LWP::UserAgent->new(); my $response = $ua->get($url); # or "head", "post", etc. $response->is_success() or die("Error fetching URL $url\n"); print($response->protocol(), ' '); print($response->status_line(), "\n"); print($response->headers_as_string());

Variation 2: Print headers of errors too

use strict; use warnings; use LWP::UserAgent; my $url = "http://www.cpan.org/"; my $ua = LWP::UserAgent->new(); my $response = $ua->head($url); # or "get", etc. print($response->protocol(), ' '); print($response->status_line(), "\n"); print($response->headers_as_string());

Variation 3: Print the whole response

use strict; use warnings; use LWP::UserAgent; my $url = "http://www.cpan.org/"; my $ua = LWP::UserAgent->new(); my $response = $ua->head($url); # or "get", etc. print($response->as_string());

Variation 4: Print redirects and all

use strict; use warnings; use LWP::UserAgent; sub print_deep { my ($response) = @_; if (my $previous = $response->previous()) { print("\n"); print_deep($previous) } print("Request\n"); print("-------\n"); print($response->request->as_string()); print("\n"); print("Response\n"); print("--------\n"); print($response->as_string()); } { my $url = "http://adaelis.com/"; # Redirects to http://www.adaelis.com/ my $ua = LWP::UserAgent->new(); my $response = $ua->get($url); # or "head", etc. print_deep($response); }

In reply to Re: collecting header data with LWP::Simple by ikegami
in thread collecting header data with LWP::Simple by coldfingertips

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.