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

Hi all,

I'm using LWP::UserAgent to post JSON data to a php script. It's working fine on my Debian machine, but from Windows XP/ActiveState Perl, the post data isn't sent.

$req=$ua->post($myurl, [$postcontent], @netscape_like_headers, 'Content-Type' => 'application/json; charset=utf-8' );
$postcontent is a string, something like:

{"slv":{"__type":"Presenters.JCMII.Views.SendLetterView","SelectedItem +Ids":[1,2,3]}}
When I watch what is being sent with Ethereal/Wireshark, it all looks correct, and works fine: I see the application/json content header, the correct content-length, then \r\n\r\n followed by the post data.

On XP, I see a content-length of zero, and no posted data.

Both machines are running Perl 5.10.0. The XP machine has LWP::UserAgent 5.826, Linux machine 5.810

The script is identical on both machines, and I've checked for silly errors (like $postcontent being empty).

Any ideas what could be causing this? Is it anything to do with the posted data not being in the more common key=value format?

Replies are listed 'Best First'.
Re: Inconsistencies posting data with LWP::UserAgent
by ikegami (Patriarch) on Jul 26, 2009 at 23:02 UTC

    You're incorrectly using the method. The referenced array should provide "the key/value pairs for the fill-in form content." I think you want the following variant:

    $ua->post( $url, $field_name => $value,... Content => $content )

    So

    $req = $ua->post( $myurl, @netscape_like_headers, 'Content-Type' => 'application/json; charset=utf-8', 'Content' => $postcontent, );