How to dynamically create LWP :: UserAgent form parameters?

I want to create a form parameter set for use in HTTP POST on the fly, but I'm not sure how to access the LWP :: UserAgent data structure dynamically.

In a typical code example, this structure is passed as a request.

my $response = $browser->post(
  'http://example.com/postme',
  [
    'param1'  => 'value1',
    'param2' => 'value2'
  ],
);

I have a set of parameter names and values ​​stored in a hash, and I want to build the structure in square brackets from my hash data. What is this structure and how can I do what I want? (as you can tell, I'm not a perl expert!)

+3
source share
1 answer

The square brackets build arrayref, but in this case the method postaccepts either arrayref or hashref. So you can just do:

my %params;
$params{param1} = 'value1'; # store parameters into %params here
my $response = $browser->post('http://example.com/postme', \%params);

perlreftut , perlref .

+5

All Articles