Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I call the Perl LWP Post function with a string parameter

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?

like image 979
kaj66 Avatar asked Mar 23 '26 12:03

kaj66


1 Answers

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.

like image 85
GMB Avatar answered Mar 26 '26 04:03

GMB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!