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

Hello Monks, I want to send a POST Request to a website without URL-encoding of square brackets.
my $ua = LWP::UserAgent->new(); my $res = $ua->request(POST "http://www.example.com/test.php", [ "var1" => "foo", "var2" => "bar", "var3[id]" => "foobar" ]);
The third variable must be submitted with real square brackets, not %5B and %5D. Otherwise the web application will not understand the request. Default behavior is to URL-encode them and I have not found a way to avoid this. Can anyone tell me how I can deactivate automatic URL encoding in variable names?

Thanks in advance, Angeldust

Replies are listed 'Best First'.
Re: How to avoid URL-Encoding in LWP POST Request
by Anonymous Monk on Nov 24, 2008 at 04:20 UTC
    Otherwise the web application will not understand the request.
    That web application is broken(using broken HTTP/CGI library).

    Can anyone tell me how I can deactivate automatic URL encoding in variable names?
    Since LWP exist to do the encoding, the simplest way is not to use LWP.

      Someone will correct me if I'm wrong, I hope, but I think this is not correct. I believe the requirement to encode brackets and parens and a few other chars is relatively new (added to the specs in 2005 if I searched right) and therefore there is going to be code in the wild that still uses the old behavior which makes it no more broken than HTML v XHTML. Confusing and problematic, but not broken.

      ++ to almut below for showing how to handle it in this case.

Re: How to avoid URL-Encoding in LWP POST Request
by almut (Canon) on Nov 24, 2008 at 16:50 UTC

    A quick look at the source (HTTP/Request/Common.pm) reveals that if you pass a pre-formatted query string like this:

    #!/usr/bin/perl use LWP::UserAgent; my $ua = LWP::UserAgent->new(); my $response = $ua->post("http://www.example.com", Content => 'var1=foo&var2=bar&var3[id]=foobar' );

    it will not be touched any further by LWP. In other words, the square brackets will be sent literally in this case.

    (Tested)

Re: How to avoid URL-Encoding in LWP POST Request
by nuclon (Acolyte) on Nov 24, 2008 at 14:26 UTC

    You can try this:

    my $ua = LWP::UserAgent->new(); my $res = $ua->request(POST "http://www.example.com" Content_Type => 'form-data', Content => [ "var1" => "foo", "var2" => "bar", "var3[id]" => "foobar" ]);

    But it seems you have broken web-application, which cannot parse requests well. Do you use CGI.pm for it?