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
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";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With