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

Hi I am trying to post some form data to a server and retrieve the result. I am trying to build the header using associative array. However I am getting error. Could someone tell me what is wrong with the code? Thanks.
my %hiddenhash; $hiddenhash{$formname1} = "formvalue1"; $hiddenhash{$formname2} = "formvalue2"; my $request = POST "http://www.server.com", Content => [ %hiddenhash + ]; my $response = $ua->request($request);

Replies are listed 'Best First'.
Re: Post with parameters using hash
by arhuman (Vicar) on Mar 05, 2001 at 22:03 UTC
    try :
    my $request = POST "http://www.server.com", \%hiddenhash ;

    From perldoc LWP another way:
    # Create a user agent object use LWP::UserAgent; $ua = new LWP::UserAgent; $ua->agent("AgentName/0.1 " . $ua->agent); # Create a request
    *****
    my $req = new HTTP::Request POST => 'http://www.perl.com/cgi- +bin/BugGlimpse'; $req->content_type('application/x-www-form-urlencoded'); $req->content('match=www&errors=0'); # Pass request to the user agent and get a response back my $res = $ua->request($req);
    ******
    # Check the outcome of the response if ($res->is_success) { print $res->content; } else { print "Bad luck this time\n"; }
      Is there a way to do it using hash? I can't use your method because one of the formvalue contains & and = which would cause another error.

      And I can't just do this because I have to build a form parameters using a for loop.
      $request = POST "http://www.server.com", Content => [ $formname1 => ' +formvalue1', $formname2 => 'formvalue2' ];
      I have to build the parameters first using hash and do something like this. But this doesn't seem to be working.
      $request = POST "http://www.server.com", Content => [ %hiddenhash ];
Re: Post with parameters using hash
by chromatic (Archbishop) on Mar 05, 2001 at 22:17 UTC
    Remove the 'Content =>' portion. See perldoc lwpcook and the second example in the POST section.

    In the future, it would be helpful to see the exact error message you've received.

    Update: Since we can't tell what's in %hiddenhash, the best I can do is to speculate that there's an extra value in there that the server doesn't like. You might use the debugger or Data::Dumper to print the hash to verify that it contains exactly and only what you think it should contain:

    use Data::Dumper; print STDERR Dumper(\%hiddenhash);
      Sorry the error is that the server is rejecting it, saying it's an invalid input.

      This code works
      $request = POST "http://www.server.com", Content => [ $formname1 => +'formvalue1', $formname2 => 'formvalue2' ];
      While this one causes the server to redirect to an error page. However, I have to do it this way, because I need to build the form parameters using a for loop.
      $hiddenhash{$formname1} = 'formvalue1'; $hiddenhash{$formname2} = 'formvalue2'; $request = POST "http://www.server.com", Content => [ %hiddenhash ];
        Funny I am having the same exact situation today, can't use a hash for the parameters.
        Tried a bunch of different ways.
        This code:
        $request = POST "http://www.server.com", Content => [ %hiddenhash ];
        simply doesn't work for me (or for the anonymous poster) Strange.. there is nothing in the hash that should be acting weird, no odd chars....does it need to be escaped or something?