Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract Headers From HTTP Request In Perl

Tags:

header

perl

I want to read headers in http or https request in my perl script :

#!/usr/bin/perl
use LWP::UserAgent;
my $ua = new LWP::UserAgent;
my $url = "http://example.com/";
my $req = $ua->get("$url");

I want To Extract The Headers Data Of The Upper Request Such As :

HTTP/1.1 200 OK
Access-Control-Allow-Headers: accept, origin, content-type, content-length
Access-Control-Allow-Methods: GET,PUT,POST,DELETE,OPTIONS
Access-Control-Allow-Origin: *
Content-Encoding: gzip
Content-Type: application/javascript
Date: Thu, 28 Apr 2016 01:43:01 GMT
Etag: "779429019"
Cookie: username=usrname; pass=P@S$W0RD;
X-Powered-By: adzerk bifrost/
x-served-by: engine-i-536776c8
like image 799
ßł Ặck Ĥặwk Avatar asked Oct 24 '25 22:10

ßł Ặck Ĥặwk


1 Answers

Note that your $req is actually a response object, not a request

To examine all of the header fields, you are looking for $resp->headers_as_string

To generate the output that you show, you would write this

#!/usr/bin/perl

use LWP::UserAgent;

my $ua = new LWP::UserAgent;
my $url = 'http://example.com/';

my $resp = $ua->get($url);

print $resp->protocol, ' ', $resp->status_line, "\n";
print $resp->headers_as_string, "\n";
like image 67
Borodin Avatar answered Oct 26 '25 19:10

Borodin



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!