Search code examples
perllwplwp-useragent

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


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?


Solution

  • 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.