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

I'm trying to work out why I don't seem to be able to pass an array of values along using LWP::UserAgent. Here is my code:

my $ua = LWP::UserAgent->new(); my $request = POST( $url, [ pdfColor => '#f7540e', proName => \@proName, proDesc => \@proDesc, amount => \@amount, price => \@price, vat => \@vat, discount => \@discount, total => \@total ] ); my $content = $ua->request($request)->as_string();


...the PHP script it calls, this is an output of what it sees:

Array ( [pdfColor] => #f7540e [proName] => test [proDesc] => Some description [amount] => 3 [price] => 30 [vat] => 0 [discount] => 0 [total] => 90 )


For some reason, the arrays I'm passing in - it only seems to get one of the entries. I've searched around, and it looks like passing in a referenced array should work just fine... but it doesn't wanna :/

UPDATE: I just came across this post:

http://stackoverflow.com/questions/20753253/perl-lwp-anoymous-array-reference-containing-array-reference-comes-across-as-arr

From the sounds of it, you can't actually pass an arrayref :( Bit rubbish, but as a work-around I am now just doing:

proDesc => join ("::::",@proDesc),

...and then decode it in the PHP code (using a "split"). Not ideal, but as long as it works =)

TIA

Andy

Replies are listed 'Best First'.
Re: LWP::UserAgent POST, pass array?
by hippo (Archbishop) on Aug 25, 2015 at 13:50 UTC
    From the sounds of it, you can't actually pass an arrayref :(

    Sure you can. Here's a simple example:

    #!/usr/bin/perl use strict; use warnings; use LWP; my $url = 'http://localhost/echo.cgi'; my $ua = LWP::UserAgent->new; my $res = $ua->post ($url, { foo => ['bar', 'baz'] }); print $res->as_string;

    Throw that at your handler and it will tell you that param "foo" has two values which are "bar" and "baz". If it doesn't, that's a bug in your handler and you'll need to head on over to PHPMonks to sort that out.

      Ah weird - not sure why its not working for me. Anyhow, I've got it working by serializing the values, and then doing a split at the other end =) Not pretty, but works <G>
Re: LWP::UserAgent POST, pass array?
by 1nickt (Canon) on Aug 25, 2015 at 13:56 UTC

    nevermind

    edit: I misunderstood your OP. I thought the values in your sample data were what you were passing in your arrayrefs, and I was going to ask why you are using arrays to begin with.

    The way forward always starts with a minimal test.