in reply to Re: libwww-perl basics
in thread libwww-perl basics

Actually, I did see that, but I thought (I really don't understand this at all) that I would have to make a second request to get just the headers.

Can I get those after $response = $ua->get..., and if so, how?

Replies are listed 'Best First'.
Re^3: libwww-perl basics
by roboticus (Chancellor) on Aug 12, 2010 at 13:13 UTC

    The docs for HTTP::Response don't give a method to access the HTTP::Headers object, but it mentions that it's a subclass of HTTP::Message. So we have to take a look at the documentation for HTTP::Message, and there we see that it provides the headers method to give you a reference to the HTTP::Headers object. So assuming $ua is an HTTP::Response object, you'd do something like this:

    for my $header (@{$ua->headers->header_field_names}) { print $header, ": ", $ua->header($header), "\n"; }

    Note: I don't write web apps, so I've never used the HTTP classes, so this is (of course!) untested. But hopefully it will lead you in the right direction.

    ...roboticus

      I tried that, and I get:

      Can't locate object method "headers" via package "LWP::UserAgent" at.. +.

      I also tried:

      for my $header (@{$response->headers->header_field_names}) { print $header, ": ", $response->header($header), "\n"; }
      and I got:
      Can't use string ("29") as an ARRAY ref while "strict refs" in use at. +..

        OK, then try it like this:

        for my $header ($response->headers->header_field_names) { ... }

        ...roboticus

      The response has the headers so s/ua/response/g