Solved

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

Posted on 2010-11-17
8
342 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
 
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
3 Use Cases for Connected Systems

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

 

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

3 Use Cases for Connected Systems

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

Question has a verified solution.

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

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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)

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now