Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl HTTP::Request HASH error?

Tags:

perl

I'm having a bit of difficulty with HTTP::Request in Perl,

My script is set up like so:

use CGI;
use MIME::Base64;
use HTTP::Cookies;
use HTTP::Request;
use LWP::UserAgent;

$request = HTTP::Request->new(GET => 'http://www.example.com/');

 $ua = LWP::UserAgent->new;
 $response = $ua->request($request);

print "$response";

When I run the script on my website, I get a message like so:

HTTP::Response=HASH(0x987f8d8)

(I'm trying to get it to "print" example.com)

like image 656
Ilan Kleiman Avatar asked Oct 23 '25 18:10

Ilan Kleiman


2 Answers

Yes its a hash. You have to access like below.

print $res->decoded_content(); ## if gziped

print $res->content();

print $res->status_line;

Also you can use the Data Dumper to print the whole $res and observe what actually it holds.

use Data::Dumper;
print Dumper $res;
like image 83
Sabuj Hassan Avatar answered Oct 26 '25 09:10

Sabuj Hassan


http::Request returns indeed a hash.

$response->content has the result.

From the link:

$r->content( $bytes ) This is used to get/set the content and it is inherited from the HTTP::Message base class. See HTTP::Message for details and other methods that can be used to access the content. Note that the content should be a string of bytes. Strings in perl can contain characters outside the range of a byte. The Encode module can be used to turn such strings into a string of bytes.

like image 38
anw Avatar answered Oct 26 '25 09:10

anw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!