views:

120

answers:

2

I find that the return from LWP::UserAgent->request() contains both the header and body of a HTTP response. I just need the body of the response to do some parsing, so how can I do?

+7  A: 

The request method (according to the manual) returns an HTTP::Response object, which has a content method. Just call that.

$ua->request->content;
David Dorward
You might actually want `decoded_content`, which deals with text encodings (in HTTP/MIME lingo, charsets) as well as HTTP `Content-Encoding`s like gzip.
hobbs
+6  A: 
require LWP::UserAgent;

my $ua = LWP::UserAgent->new;

my $response = $ua->get('http://search.cpan.org/');

if ($response->is_success) {

    print $response->decoded_content;  # or whatever
}
else {
    die $response->status_line;
}

response->decoded_content will return the body of the response.

GJ