newspaint

Documenting Problems That Were Difficult To Find The Answer To

Debugging LWP::UserAgent in Perl 5

Over the years the way of dumping the sent and received headers of HTTP requests changed. It used to be the following line added to your script would result in headers being dumped:

use LWP::Debug qw(+);

But more recently the approach has been to add handlers to the user agent, e.g.:

$ua->add_handler(
  "request_send",
  sub {
    my $msg = shift;              # HTTP::Message
    $msg->dump( maxlength => 0 ); # dump all/everything
    return;
  }
);

$ua->add_handler(
  "response_done",
  sub {
    my $msg = shift;                # HTTP::Message
    $msg->dump( maxlength => 512 ); # dump max 512 bytes (default is 512)
    return;
  }
);

The dump() function is documented in the HTTP::Message module.

The dump() function has a few options that can be provided:

  • maxlength – maximum number of bytes to display, zero for unrestricted (all)
  • no_content – a string to replace “(no content)” with
  • prefix – a string to prepend to each line dumped

The handlers available are documented in the LWP::UserAgent module and are:

  • request_preprepare
  • request_prepare
  • request_send
  • response_header
  • response_data
  • response_done
  • response_redirect

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: