Using a hash for the post data in LWP::useragent?

A

Andrew

Does anyone know of a way to send the post data as reference to a hash
instead of hardcoding the post data into the useragent request? I
have tried using HTTP::Request::Common and Useragent request.

for example:

$ua->request(POST 'www.someurl.com',
[ username => 'bob',
host => 'localhost',
other => '1',
other2 => '2',
other3 => '3',
]);

I would like to to be able to use the same subroutine for multiple
post data hashes by calling

my %posthash = [ username => 'bob',host => 'localhost', other => '1',
other2 => '2', other3 => '3']


$ua->request(POST $url, \%posthash); # something like this



anyone have any ideas on how to do this?
 
G

Gunnar Hjalmarsson

Andrew said:
Does anyone know of a way to send the post data as reference to a
hash instead of hardcoding the post data into the useragent
request? I have tried using HTTP::Request::Common and Useragent
request.

for example:

$ua->request(POST 'www.someurl.com',
[ username => 'bob',
host => 'localhost',
other => '1',
other2 => '2',
other3 => '3',
]);

Assuming that is correct syntax, you are passing an array reference,
not a hash reference.
I would like to to be able to use the same subroutine for multiple
post data hashes by calling

my %posthash = [ username => 'bob',host => 'localhost',
other => '1', other2 => '2', other3 => '3']

$ua->request(POST $url, \%posthash); # something like this

Try this:

my $postarrayref = [ username => 'bob',host => 'localhost',
other => '1', other2 => '2', other3 => '3' ];

$ua->request(POST $url, $postarrayref);

(untested)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,081
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top