0

I need to submit a HTTP POST request to a certain URL, and I am required to specify a parameter name that can be interpreted as an array - like this:

parameter[]=123

However, no matter what I try, LWP is always escaping [] characters.

Here is sample code:

#!/usr/bin/perl

use strict;
use warnings;
use LWP;
use HTTP::Request::Common;
$|=1;

my $ua = LWP::UserAgent->new;
my $post_url = "http://192.168.1.1/something";

my $params = { };
$params->{something} = "abc";
$params->{'array[]'} = 123;
my $response = $ua->request(POST $post_url, [ $params ]);

Data submitted looks like this:

POST /something HTTP/1.1
TE: deflate,gzip;q=0.3
Connection: TE, close
Host: 192.168.1.1
User-Agent: libwww-perl/5.835
Content-Length: 29
Content-Type: application/x-www-form-urlencoded

array%5B%5D=123&something=abc

And I need it to look like this:

POST /something HTTP/1.1
TE: deflate,gzip;q=0.3
Connection: TE, close
Host: 192.168.1.1
User-Agent: libwww-perl/5.835
Content-Length: 25
Content-Type: application/x-www-form-urlencoded

array[]=123&something=abc

I have no control over remote application and can not influence anything, I simply have to specify a single parameter like this (as a parameter of an array, which it really isn't), and I need to find a way how to do this, without Perl escaping bracket characters.

I have tried defining 'array' as array and arrayref (and many other things), but LWP does not seem to understand the concept of array parameters, even if I have multiple values for this parameter, they will all be submitted with same parameter name (?array=123&array=456&array=789) - so that won't work either.

Mostly, I am wondering if I can somehow (short of modifying module sources) prevent LWP to automatically escape these characters when making a POST request.

Thanks.

sentinel
  • 249
  • 2
  • 10
  • 2
    The Internet works because we're all playing by the same [rules](http://en.wikipedia.org/wiki/Request_for_Comments); specifically documents conforming to the `application/x-www-form-urlencoded` content-type [*must* undergo a specific encoding](http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1). Put pressure on the receiving side to fix their moronic parser, anything else is a hack. – daxim Mar 31 '11 at 13:14

3 Answers3

2

You are sending a message whose content is form-urlencoded. Try creating first an unencoded request:

use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(POST => 'http://192.168.1.1/something');
$req->content('array[]=123&something=abc');
my $res = $ua->request($req);
Francisco R
  • 4,032
  • 1
  • 22
  • 37
  • This is what I did last night and it works fine, so I am accepting this answer now, although daxim's solution is probably more fun to play with ;) – sentinel Apr 01 '11 at 07:24
1

Overriding parts of $URI::Escape::escapes as detailed in How may I bypass LWP's URL encoding for a GET request? should work for this too.

Community
  • 1
  • 1
Lasse
  • 686
  • 4
  • 9
0

Override the method URI::_query::query_form, called from HTTP::Request::Common::POST.

daxim
  • 39,270
  • 4
  • 65
  • 132