Solved

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

Posted on 2010-11-17
8
327 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
Comment Utility
$h->as_string()
0
 

Author Comment

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

Author Comment

by:php-newbie
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:php-newbie
Comment Utility
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
Comment Utility
->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
Comment Utility
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
Comment Utility
perfect, thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

771 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

12 Experts available now in Live!

Get 1:1 Help Now