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

I am using LWP::UserAgent to send a POSt request to a remote server.
my $type = 'post'; my $url = redacted; my $headers = { 'Content' => { 'field1' => '1', 'field2' => [{'subfield1' => 'x', 'subfield2' => 'y'}, possibly mo +re in array], 'field3' => {subfield3 => int, 'subfield4' => 'string'}, }, } my $response = $ua->$type( $url, %{$headers} );
For testing I also added in
$headers->{Content}->{'foo'} = ['bar', 'baz']
I get an error back and it looks like my hash reference and array ref for field2 and field3 and passed as strings not the underlying objects so the _content string looks like The foo part parses correctly which tells me that LWP or HTTP::Request::Common POST doesn't like converting the hashes.
"field3": "HASH(0x2308c10)"
Obviously this isn't what I want. Now previously I had solved this by doing horrible things with strings.
$args = {data => $args}; my @newstring; foreach my $key (keys %{$args}) { foreach my $value (keys %{$args->{$key}}) { push @newstring, $key.'['.$value.']='.$args->{$key}->{$value}; } } $args = join('&',@newstring); my $response = $ua->post( $url, Content => $args );
That was in a different revision of the remote server when it wanted things in a data structure variable. Essentially I was crafting my own string _content string and passing it in. The server understood it but it was a ugly way of doing it. I'm looking for a solution that doesn't involve me having to construct the string myself

Replies are listed 'Best First'.
Re: LWP::UserAgent POST pass hash ( PHP::HTTPBuildQuery http_build_query )
by beech (Parson) on Mar 04, 2016 at 23:16 UTC

     'field2' => [{'subfield1' => 'x', 'subfield2' => 'y'}, possibly more in array],

    See HTTP::Request::Common, LWP doesn't accept/process hash refs , it doesn't have a concept of "subfields", like a regular html form its just key/value pairs. Specifically LWP* wants a flat list like this

    [ 'field1' => '1', 'field2{subfield1]' => 'x', 'field2[subfield2]' => 'y', 'field3{subfield3]' => 'int', 'field3{subfield4}' => 'string', ]

    What you're looking for is like the opposite of CGI::Struct , it is PHP::HTTPBuildQuery / http_build_query

    update: An example of a round trip with PHP::ParseStr/php_parse_str :)

    update: and now with "LWP"

    #!/usr/bin/perl -- use strict; use warnings; use PHP::HTTPBuildQuery qw/ http_build_query /; use PHP::ParseStr qw( php_parse_str ); use Data::Dump qw/ dd /; use WWW::Mechanize; my $con = { 'field1' => '1', 'field2' => [ { 'subfield1' => 'x', 'subfield2' => 'y' }, ], 'field3' => { subfield3 => 'int', 'subfield4' => 'string' }, }; my $hbq = http_build_query($con); my $pps = php_parse_str($hbq); dd( $con, $hbq, $pps ); my $ua = WWW::Mechanize->new( autocheck => 0 ); $ua->timeout(0.1); $ua->add_handler("request_send", sub { shift->dump; return }); #~ $ua->add_handler("response_done", sub { shift->dump; return }); #~ $ua->show_progress(1); $ua->post( 'http://localhost/', Content => $hbq, ); $ua->post( 'http://localhost/', Content => [ "field1" => 1, "field2[0][subfield1]" => "x", "field2[0][subfield2]" => "y", "field3[subfield3]" => "int", "field3[subfield4]" => "string", ], ); __END__ ( { field1 => 1, field2 => [{ subfield1 => "x", subfield2 => "y" }], field3 => { subfield3 => "int", subfield4 => "string" }, }, "field1=1&field2%5B0%5D%5Bsubfield2%5D=y&field2%5B0%5D%5Bsubfield1%5 +D=x&field3%5Bsubfield3%5D=int&field3%5Bsubfield4%5D=string", { field1 => 1, field2 => [{ subfield1 => "x", subfield2 => "y" }], field3 => { subfield3 => "int", subfield4 => "string" }, }, ) POST http://localhost/ Accept-Encoding: gzip User-Agent: WWW-Mechanize/1.73 Content-Length: 125 Content-Type: application/x-www-form-urlencoded field1=1&field2%5B0%5D%5Bsubfield2%5D=y&field2%5B0%5D%5Bsubfield1%5D=x +&field3%5Bsubfield3%5D=int&field3%5Bsubfield4%5D=string POST http://localhost/ Accept-Encoding: gzip User-Agent: WWW-Mechanize/1.73 Content-Length: 125 Content-Type: application/x-www-form-urlencoded field1=1&field2%5B0%5D%5Bsubfield1%5D=x&field2%5B0%5D%5Bsubfield2%5D=y +&field3%5Bsubfield3%5D=int&field3%5Bsubfield4%5D=string
      Well that would explain why I couldn't achieve the desired results with LWP. PHP just always has to make it weird. Thanks beech. I spent a few days trying to figure this out.

      Let me rewrite my application/script with this and make sure the devs on the php app haven't done even stranger things.

      Edit: Rewrote my script and it works wonderfully.