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

Hello Monks

I have an odd problem that I can't seem to resolve involving LWP::UserAgent. I am trying to pass a simple POST to a URL. The URL requires certain fields and has a date type of "array".

To do this, in Curl, say, I wrap the value(s) in square brackets []. However, when I do this in LWP::UserAgent the sqaure bracket gets encoded and the POST fails!

Does anyone know how to pass a content field in LWP::UserAgent and keep the string exactly as is?

e.g - entities value is the problem. Curl first (works).

curl -X POST "https://mysite.com/lookup" \ -d '{"name":"respond", "falvour": green, "location": europe, "enti +ties":[369662]}' \ -H "Content-Type: application/json"

LWP::UserAgent example (failing)

my $ua = LWP::UserAgent->new; $ua->default_header('Content-Type' => 'application/json'); $data = { name => "responding", flavour => "green", entities => '[369662]' , location => "europe", }; my $response = $ua->post( 'https://mysite.com/lookup', $data

The latter results in the string being passed as "....&entities=%5B369662%5D", which gets rejected.

Replies are listed 'Best First'.
Re: LWP content literal string
by Corion (Patriarch) on Oct 13, 2014 at 16:25 UTC

    If you want to pass a JSON string to the other side, don't use the HTTP::Request::Common methods that create an application/x-www-form-urlencoded string (and are documented to do so).

    Simply create the string using (say) JSON::XS and then use the Content parameter of HTTP::Request to put your string there.

      I did try that but I get an 400 message : "'Library does not allow method HTTP::Request=HASH(0xa16e5d8) for \'http:\' URLs'"

      I can't track the meaning of that message down anywhere

        This message means you messed up the request, there is no such method as "HTTP::Request=HASH(0xa16e5d8)"