0

我需要向HTTP POST某个 URL 提交请求,并且我需要指定一个可以解释为数组的参数名称 - 像这样:

parameter[]=123

但是,无论我尝试什么,LWP 总是转义 [] 字符。

这是示例代码:

#!/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 ]);

提交的数据如下所示:

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

我需要它看起来像这样:

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

我无法控制远程应用程序,也无法影响任何事情,我只需要指定一个像这样的参数(作为数组的参数,实际上不是),我需要找到一种方法来做到这一点, 没有 Perl 转义括号字符。

我尝试过定义'array'asarrayarrayref(以及许多其他的东西),但是 LWP 似乎不理解数组参数的概念,即使我有这个参数的多个值,它们都会以相同的参数名称 ( ?array=123&array=456&array=789) 提交 - 这样也不会工作。

大多数情况下,我想知道我是否可以以某种方式(没有修改模块源)阻止 LWP 在发出 POST 请求时自动转义这些字符。

谢谢。

4

3 回答 3

2

您正在发送一条内容为 form-urlencoded 的消息。尝试首先创建一个未编码的请求:

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);
于 2011-03-31T14:04:45.500 回答
1

覆盖部分$URI::Escape::escapes详细信息,详见如何绕过 LWP 的 GET 请求 URL 编码?也应该为此工作。

于 2014-02-25T21:00:43.910 回答
0

覆盖从 HTTP::Request::Common::POST 调用的方法URI::_query::query_form

于 2011-03-31T13:21:34.250 回答