Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

HTTP Headers Using WWW::Mechanize

by Limbic~Region (Chancellor)
on Jan 31, 2005 at 18:04 UTC ( #426681=snippet: print w/replies, xml ) Need Help??
Description: It isn't readily apparent from the docs how to get at the HTTP headers while using WWW::Mechanize. The miscellaneous add_header and delete_header likely don't do what you want. The following snippet gets you a complete list of headers and their values as well as shows you how to get a specific value if you know the header key in advance.
use strict;
use warnings;
use WWW::Mechanize;

my $mech = WWW::Mechanize->new( autocheck => 1 );
$mech->get( '' );

my $response = $mech->response();
for my $key ( $response->header_field_names() ) {
    print $key, " : ", $response->header( $key ), "\n";
# Or if you know the header key in advance
my $header = $mech->response()->header( 'key' );
Replies are listed 'Best First'.
Re: HTTP Headers Using WWW::Mechanize
by holli (Monsignor) on Jan 31, 2005 at 18:26 UTC
    did you think of sending that as a documentation-patch to Andy Lester?

    holli, regexed monk
      Thanks. I thought of that as well and had already /msg'd him with a pointer to this thread. Not sure if Andy would prefer to add more miscellaneous methods, modify the documentation, add to the FAQ, or something else entirely.

      Cheers - L~R

Re: HTTP Headers Using WWW::Mechanize
by merlyn (Sage) on Jan 31, 2005 at 19:48 UTC
      What's wrong with: $mech->response->headers->as_string?

      Nothing, except it solves a problem other than the one I was going after (see Description).

      First, the docs for WWW::Mechanize leave a little to be desired as far as getting at the headers in the first place. You have to know that the HTTP::Response object returned by the response() method has what you are looking for.

      Second, the intention by accessing them by key was just to get the value you wanted as per the Description. In this particular case, water was after the p3p key. Of course you could parse the results of the as_string method, but why?

      Cheers - L~R

        Patches welcome. If something's unclear, please send me updates to the doc that expand on it. It's hard for me, as the author, to know what people need to know, or if it's not as clear as should be.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://426681]
[marto]: more so than the previous show I saw
[Corion]: marto: Well, I think they go a tour every two years and I think it's hard to even get a connection with the crowd at a 20k people concert... But maybe after this time I'll stop too ;)
[Corion]: I still have to see the Pet Shop Boys live before they stop touring at all
[marto]: yeah, I think that as a group creatively they're done. I can understand how it'd be hard to stop the process, album/tour, album/tour, if that's pretty much all you've ever done :)
[marto]: Corion yes I saw them Pandemonium_Tour
[Corion]: marto: Yeah, and I doubt that they'll ever get back to something like Violator/Songs of Faith and Devotion - it would either alienate their "regular" crowd, or be "too much Violator" ;)
LanX needs to see Freddy Mercury'
[Corion]: marto: Yeah, I have that on DVD even, and I missed them last year because I was in London at the time ironically ;)
[marto]: put me in the mood to listen to this now :)
[Corion]: LanX: Well, grab a shovel :)

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (12)
As of 2017-03-24 11:41 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (301 votes). Check out past polls.