Solved

Gettinga HTTP::Headers from a  HTTP::Request or  HTTP::Response object (Perl)

Posted on 2010-11-17
8
359 Views
Last Modified: 2013-12-25
Experts,

how can I get a HTTP::Headers object from a HTTP::Request or a HTTP::Response object?

I know I can get single fields from the Request or Response obj but I'd like to print out the complete header (e.g. $header->as_string) and don't know in advance which header fields might be present.

Thanks!
0
Comment
Question by:php-newbie
  • 4
  • 3
8 Comments
 
LVL 17

Expert Comment

by:sweetfa2
ID: 34158652
$h->as_string()
0
 

Author Comment

by:php-newbie
ID: 34158680
yes, but how do I get the Header object from the Request or response object?
0
 

Author Comment

by:php-newbie
ID: 34158695
so for example if I have the code below

my $http_header = HTTP::Headers->new();
my $http_request = HTTP::Request->new();
my $http_response = HTTP::Response->new();

$http_request->content($message);
$http_response = $ua->request($http_request);

How would I set the http_header to the http_request or http_response variables?
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 17

Expert Comment

by:sweetfa2
ID: 34158705
as_string without parameters uses the scan callback method to feed all the header details in.

Otherwise, you can use $h->scan(callback) with a callback method that takes the header and it's value.  The callback will get called for each header value.

http://www.xav.com/perl/site/lib/HTTP/Headers.html
0
 

Author Comment

by:php-newbie
ID: 34158769
I'm sorry, I must not be very clear :)

so if I have the header variable like $h in your example. How do I 'point it' to the corresponding request or response variable?
0
 
LVL 17

Accepted Solution

by:
sweetfa2 earned 500 total points
ID: 34158856
->headers

It is a base class of the request and response message types.

http://kobesearch.cpan.org/htdocs/libwww-perl/HTTP/Message.html#mess_gt_headers
0
 
LVL 16

Expert Comment

by:jmatix
ID: 34158891
Since you are constructing the Request object you already have the headers as:

require HTTP::Headers;
$h = HTTP::Headers->new;
$h->header('Content-Type' => 'application/octet-stream');  # set header
$r = HTTP::Request->new('PUT', "$url", $h);
$r->content($cont);
$ua = LWP::UserAgent->new;
$response = $ua->request($r);
print $h->as_string;


But if you are receiving a Response object you can print all the headers as below:

require LWP::UserAgent;
use HTTP::Request;
$ua = new LWP::UserAgent;
$ua->agent('Mozilla/4.0 (compatible; MSIE 5.0; Windows NT)');
$h = HTTP::Headers->new;
$r = HTTP::Request->new('GET', "http://www.google.com/", $h);

$response = $ua->request($r);
if ($response->is_success) {
   ($resp=$response->as_string) =~ s/\n\n.+//s;
    print  $resp;   #print response headers
}
else {
    print STDERR $response->status_line, "\n";
}

0
 

Author Comment

by:php-newbie
ID: 34158919
perfect, thanks!
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question