Solved

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

Posted on 2010-11-17
8
368 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

734 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