Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: LWP::UserAgent POST pass hash ( PHP::HTTPBuildQuery http_build_query )

by beech (Parson)
on Mar 04, 2016 at 23:16 UTC ( [id://1156861]=note: print w/replies, xml ) Need Help??


in reply to LWP::UserAgent POST pass hash

 '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

Replies are listed 'Best First'.
Re^2: LWP::UserAgent POST pass hash ( PHP::HTTPBuildQuery http_build_query )
by Stoney2005 (Acolyte) on Mar 07, 2016 at 12:59 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1156861]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-25 08:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found