3

I have a problem supplying a parameter list to the UserAgent Post function (using the https module). The examples are of the form:

my $ua = LWP::UserAgent->new();
my $response = $ua->post( $url, { 'param1', 'value1', 'param2', 'value2' } );

The API I want to access accepts parameters using duplicate key names, where the order of parameters is important. For example:

https://URL?feature_name='animal'&feature_value='dog'&feature_name='vehicle'&feature_value='boat'

I can't pass this to the POST function as a hash because of the duplicate key names. Is it possible to pass the parameters as a string instead?

GMB
  • 216,147
  • 25
  • 84
  • 135
kaj66
  • 153
  • 1
  • 11

1 Answers1

4

LWP::UserAgent supports passing parameters as an array reference rather than a hash ref, so you can just do:

my $ua = LWP::UserAgent->new();
my $response = $ua->post( $url, [ 
    'feature_name',  'animal', 
    'feature_value', 'dog',
    'feature_name',  'vehicle', 
    'feature_value', 'boat' 
] );

Another supported option is to use a hash of array references:

my $response = $ua->post( $url, [ 
    feature_name  => [qw/animal vehicle/],
    feature_value => [qw/dog boat/]
] );

For more details about supported options, you can have a look at the documentation of HTTP::Request::Common, in the POST section, that goes like:

Multivalued form fields can be specified by either repeating the field name or by passing the value as an array reference.

GMB
  • 216,147
  • 25
  • 84
  • 135