How can I get the body of an HTTP response using LWP::UserAgent in Perl?

Haiyuan Zhang picture Haiyuan Zhang · Dec 17, 2009 · Viewed 10.4k times · Source

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?

Answer

GJ. picture GJ. · Dec 17, 2009
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.