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

In an effort to stop my wall from getting a bigger hole in it from my head, I post this question.. In the below code, I am posting HTTPS to a server with both headers and name/value pairs. I cannot for the life of me, get the headers to go in any way shaped or form. If I try to send the headers via "$headers (using HTTP::Headers), I get the error: Not an ARRAY reference at C:/Perl/lib/HTTP/Request/Common.pm line 82. Ok, so I change it to an array using @headers, no error this time, however, the name/value pairs do not get submitted along with the post, so I get errors in the response. If I remove the @headers from the POST line, I get the correct reponse from the server, but, of course I did not send any headers. Please help!!
#!/ActivePerl-.10/bin/perl use LWP 5.69; use HTTP::Request::Common qw( POST ); use HTTP::Headers; use HTTP::Response; my $submit_values = { #name/value pairs here + }; + + my $headers = HTTP::Headers->new( + 'Timeout' => '30', + ); + my @headers = ( + 'Timeout','30', + ); + my $ua = LWP::UserAgent->new( protocols_allowed => ['https'] ); + my $r = POST( 'https://someserver.com',@headers,$submit_values); + my $myresponse = $ua->request( $r ); $apiresponse = $myresponse->content ; print $apiresponse; exit;

Replies are listed 'Best First'.
Re: LWP: POST with headers
by shmem (Chancellor) on Jun 04, 2009 at 00:05 UTC

    Try

    my $headers = HTTP::Headers->new( 'Timeout' => '30', ); $ua->default_headers($headers);
      Your code did the trick, thank you, thank you, thank you!
Re: LWP: POST with headers
by almut (Canon) on Jun 04, 2009 at 00:20 UTC

    Alternatively, try

    my $r = POST( 'https://someserver.com', $submit_values); $r->header(Timeout => '30');
      Thank you for your reply, I had tried this method, but the PERL program just hung and did not complete.. not sure what else I was doing wrong. However, I did get the code to work via another poster.. Thanks again for taking the time to respond.