How can I get the body of an HTTP response using LWP::UserAgent in Perl?
11,122
Solution 1
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.
Solution 2
The request
method (according to the manual) returns an HTTP::Response object, which has a content
method. Just call that.
$ua->request->content;
Author by
Haiyuan Zhang
Updated on September 05, 2022Comments
-
Haiyuan Zhang over 1 year
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? -
hobbs over 14 yearsYou might actually want
decoded_content
, which deals with text encodings (in HTTP/MIME lingo, charsets) as well as HTTPContent-Encoding
s like gzip.