giulienk has asked for the wisdom of the Perl Monks concerning the following question:

I'm no expert in perl objects. How can i set the HTTP:Headers object in the $req I got using
use HTTP::Request::Common qw(POST); use Data::Dumper; $req = POST 'http://gino/ciccio.cgi', [ sessionid => 'something', mittente => 'something', prefix => 'number', numtel => 'number', messagetext => 'message', flash => "0", Submit => " Invia " ]; print Dumper($req);
The output looks like this
$VAR1 = bless( { '_method' => 'POST', '_headers' => bless( { 'content-type' => 'application +/x-www-form-urlencoded', 'content-length' => 109 }, 'HTTP::Headers' ), '_uri' => bless( [ bless( do{\(my $o = 'http://gino/c +iccio.cgi' )}, 'URI::http' ), undef ], 'URI::URL' ), '_content' => 'sessionid=something&mittente=something +&prefix=number&numtel=number&messagetext=message&flash=0&Submit=+Invi +a+' }, 'HTTP::Request' );
I see there is a HTTP:Headers object but i'm no really good in assigning anything to it or even display its content: can anybody help me? Thank you.

Replies are listed 'Best First'.
Re: Setting HTTP:Headers using HTTP::Request::Common
by blakem (Monsignor) on Sep 24, 2001 at 12:06 UTC
    Have you looked at the pod documentation for that module?
    % perldoc HTTP::Request::Common
    It has an example that appears to do exactly what you want:
    POST 'http://www.perl.org/survey.cgi', Content_Type => 'form-data', Content => [ name => 'Gisle Aas', email => 'gisle@aas.no', gender => 'M', born => '1964', init => ["$ENV{HOME}/.profile"], ]

    -Blake

Re: Setting HTTP:Headers using HTTP::Request::Common
by Zaxo (Archbishop) on Sep 24, 2001 at 12:09 UTC

    You can pass the form data as an array ref of pairs, as you've done, then the http headers as the same in a third argument:

    $req = POST 'http://gino/ciccio.cgi', [ sessionid => 'something', mittente => 'something', prefix => 'number', numtel => 'number', messagetext => 'message', flash => "0", Submit => " Invia " ], [ 'Header1' => "header1thing", 'Header2' => "header2thing" ];
    $req is a perfectly good HTTP::Request object.

    After Compline,
    Zaxo

(dkubb) Re: (1) Setting HTTP:Headers using HTTP::Request::Common
by dkubb (Deacon) on Sep 24, 2001 at 12:15 UTC

    When you use HTTP::Request::Common's POST routine, it creates an HTTP::Request object that has all it's attributes set to the most common values. For a large majority of tasks you will not need to set the values to anything different, and if you do, it is a great point at which to start from.

    Now, HTTP::Message is the parent to HTTP::Request, and passes down it's headers method to it (along with all it's other methods) which will let you get at this request's HTTP::Headers object.

    Here is your example, modified to show the use of this method:

    #!/usr/bin/perl -w use strict; use HTTP::Request::Common qw(POST); #build a pre-fab HTTP::Request object my $request = POST 'http://gino/ciccio.cgi', [ sessionid => 'something', mittente => 'something', prefix => 'number', numtel => 'number', messagetext => 'message', flash => 0, Submit => ' Invia ', ]; #get this request's HTTP::Headers object my $headers = $request->headers; #you can call any method specified in HTTP::Headers' perldoc print $headers->as_string;
      Thank you very much! Following your advice now i can set the referer to what i want as
      $request->headers->referer($previous_page);
      Now, my script works :)