Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-11-17
8
Medium Priority
?
388 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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:Justin Mathews
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

610 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