Hi,

I am struggling with trying to understand how to loop through (or even access) headers returned from an HTTP request.

My code is like this:

use strict; use LWP::Simple; require LWP::UserAgent; my $ua = LWP::UserAgent->new; $ua->timeout(10); $ua->env_proxy; $ua->max_redirect(0); my $response = $ua->get('http://www.msn.co.uk'); if ($response->is_success) { while (my ($x,$y) = each (%$response)) { print "$x - $y\n"; } } else { die $response->status_line; }

Since the response is a 301, and I have redirects set to zero, then it's easy enough to use Data Dumper to see what the response is, per below:

$VAR1 = bless( { '_protocol' => 'HTTP/1.1', '_content' => '', '_rc' => '301', '_headers' => bless( { 'x-powered-by' => 'ASP.NET', 'client-response-num' => 1, 'cache-control' => 'no-cache', 'set-cookie' => 'MC1=V=3&GUID=e03d ....', 'location' => 'http://uk.msn.com/', 'date' => 'Mon, 27 Sep 2010 15:31:10 GMT', 'client-peer' => '213.199.164.24:80', 'content-length' => '0', 'x-aspnet-version' => '2.0.50727', 'p3p' => 'CP="BUS CUR CONo FIN IVDo ONL ..."', 'client-warning' => 'Redirect loop detected (max_redirect = +0)', 'client-date' => 'Mon, 27 Sep 2010 15:31:54 GMT', 'pragma' => 'no-cache', 'server' => 'Microsoft-IIS/6.0', 's:' => '04' }, 'HTTP::Headers' ), '_msg' => 'Moved Permanently', '_request' => bless( { '_content' => '', '_uri' => bless( do{\(my $o = 'http://www.msn.co.uk')}, 'UR +I::http' ), '_headers' => bless( { 'user-agent' => 'libwww-perl/5.803' }, 'HTTP::Headers' ), '_method' => 'GET' }, 'HTTP::Request' ) }, 'HTTP::Response' );

However, I still can't figure out how to access it.

If I set the redirect to 1, then I'll end up with a bunch of content returned, and a response that will include:

_protocol - HTTP/1.1 _content - bunch of content _rc - 200 _headers - HTTP::Headers=HASH(0x1cde8fc) _previous - HTTP::Response=HASH(0x1cc618c) _msg - OK _request - HTTP::Request=HASH(0x1c7e310)

However, I simply do not understand how to access any of the data, whichever way it gets returned. I want to be able to process several different URLs, and whether it's a 301 response or 200, or whatever, I would like to be able to loop through the data in the headers and also grab the response code, protocol, and msg. I don't need to know what the content is. I have spent hours trying to figure this out but really seem to have no clue. Any assistance would be much appreciated.


In reply to Looping through HTTP headers by Anonymous Monk

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.