防止 LWP 转义请求参数名称中的某些字符

发布于 2024-10-28 08:12:02 字数 1410 浏览 1 评论 0原文

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

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' 定义为 arrayarrayref (以及许多其他东西),但 LWP 似乎不理解数组的概念参数,即使我有这个参数的多个值,它们都将以相同的参数名称提交(?array=123&array=456&array=789) - 所以这也不起作用。

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

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

提笔落墨 2024-11-04 08:12:02

您正在发送一条内容经过 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);

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);
ら栖息 2024-11-04 08:12:02

覆盖 $URI::Escape::escapes 的部分内容,详见 我如何绕过 GET 请求的 LWP URL 编码? 也应该适用于此。

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.

记忆消瘦 2024-11-04 08:12:02

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文