Perl LWP anonymous array reference containing array reference comes across as array with a single element

StackOverflow https://stackoverflow.com/questions/20753253

  •  21-09-2022
  •  | 
  •  

Question

I'm having a small issue. I'm using LWP::useragent and post to another script. In that script I am performing some logic on a json string and multiple files passed in anonymous array. Everything was going fine until I attempted to push the multiple files I was passing into an array and pass that as an array reference within the anonymous array.

open (IMAGE, "./flower.jpg") or die "$!";
open (IMAGE2, "./fw4.pdf") or die "$!";

$raw_string1 = do{ local $/ = undef; <IMAGE>; };
$raw_string2 = do{ local $/ = undef; <IMAGE2>; };


my @file_array;

push(@file_array, $raw_string1);
push(@file_array, $raw_string2);

my $array_ref = \@file_array;

my $data = [json_string => $json, file_array => $array_ref];
my $ua = LWP::UserAgent->new;
$res = $ua->request(POST($url, $data));

On the catch script I read the params being passed from the anonymous into a hash. I'm able to access the json string passes without issue like:

my $json_post = $params{'json_string'};

And then I decode it and do what I wish with it it's all good. So I figured I could access the array ref like:

 my $array_ref = $params{'file_array'};
 my @array = @$array_ref;

also tried

my @array = @{$array_ref};
Was it helpful?

Solution

You can only send a stream of bytes over a socket. Anything else must be serialized into a stream of bytes and deserialized on the remote end.

Your opted to serialize using the application/x-www-form-urlencoded protocol. It's only capable of serializing key-value pairs of strings. Yet you try to pass a reference.

You'll need to serialize the contents of the array into a string in a manner expected by the server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top