Recently I found myself trying to debug a problem with WWW::Mechanize and I wanted to dump the headers as my Mech object was traversing several Web pages with complicated redirects. The following subclass will print the headers to STDOUT while the requests are being made (note that this may be undesireable behavior and you'll probably want to adjust this to suit your own needs).

package WWW::Mechanize::WithHeaders; use base 'WWW::Mechanize'; use constant REQUEST => 'Request'; use constant RESPONSE => 'Response'; sub _make_request { my $self = shift; $self->_show_headers(REQUEST, $_[0]->as_string); my $response = $self->SUPER::_make_request(@_); $self->_show_headers(RESPONSE, $response->as_string); return $response; } sub _show_headers { my ($self, $type, $headers) = @_; if (RESPONSE eq $type) { $headers =~ s/\n\n.*$/\n\n/s; # strip body } else { $headers .= "\n"; # add an extra newline } print "$type:\n\n$headers"; $self; }

Replies are listed 'Best First'.
Re: Show WWW::Mechanize Headers
by LTjake (Prior) on Dec 11, 2003 at 20:06 UTC

    If you really just want to see what's going on with LWP and don't need ALL of the headers, use LWP::Debug

    use strict; use warnings; use diagnostics; use LWP::Debug qw( + ); use WWW::Mechanize; my $a = WWW::Mechanize->new(); my $url = 'http://diveintomark.org/tests/client/http/307.xml'; $a->get( $url ); print length( $a->content );

    Gives me:

    LWP::UserAgent::new: () LWP::UserAgent::request: () HTTP::Cookies::add_cookie_header: Checking diveintomark.org for cookie +s HTTP::Cookies::add_cookie_header: Checking .org for cookies LWP::UserAgent::send_request: GET http://diveintomark.org/tests/client +/http/307.xml LWP::UserAgent::_need_proxy: Not proxied LWP::Protocol::http::request: () LWP::UserAgent::request: Simple response: Temporary Redirect LWP::UserAgent::request: () HTTP::Cookies::add_cookie_header: Checking diveintomark.org for cookie +s HTTP::Cookies::add_cookie_header: Checking .org for cookies LWP::UserAgent::send_request: GET http://diveintomark.org/tests/client +/http/307_redirect.xml LWP::UserAgent::_need_proxy: Not proxied LWP::Protocol::http::request: () LWP::Protocol::collect: read 1003 bytes LWP::UserAgent::request: Simple response: OK 1003

    --
    "To err is human, but to really foul things up you need a computer." --Paul Ehrlich