如何使用 LWP 发布数组形式
我在创建一个可以使用 LWP 作为表单传递的数组时遇到问题。基本代码是
my $ua = LWP::UserAgent->new();
my %form = { };
$form->{'Submit'} = '1';
$form->{'Action'} = 'check';
for (my $i=0; $i<1; $i++) {
$form->{'file_'.($i+1)} = [ './test.txt' ];
$form->{'desc_'.($i+1)} = '';
}
$resp = $ua->post('http://someurl/test.php', 'Content_Type' => 'multipart/form-data'
, 'Content => [ \%form ]');
if ($resp->is_success()) {
print "OK: ", $resp->content;
}
} else {
print $claimid->as_string;
}
我想我没有正确创建表单数组或使用错误的类型,因为当我检查 test.php 中的 _POST 变量时,没有设置任何内容:(
I am having problems creating an array that I can pass as a form using LWP. Basic code is
my $ua = LWP::UserAgent->new();
my %form = { };
$form->{'Submit'} = '1';
$form->{'Action'} = 'check';
for (my $i=0; $i<1; $i++) {
$form->{'file_'.($i+1)} = [ './test.txt' ];
$form->{'desc_'.($i+1)} = '';
}
$resp = $ua->post('http://someurl/test.php', 'Content_Type' => 'multipart/form-data'
, 'Content => [ \%form ]');
if ($resp->is_success()) {
print "OK: ", $resp->content;
}
} else {
print $claimid->as_string;
}
I guess I am not creating the form array correctly or using the wrong type as when I check the _POST variables in test.php nothing has been set :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是,由于某种原因,您将表单值括在单引号中。您想要发送数据结构。例如:
您想要发送
%form 的哈希引用,而不是像您那样发送包含在数组引用中的 has 引用 (
[ \%form ])。如果您想将数据作为数组引用发送,那么您只需使用
[ %form ]`,它用哈希中的键/值对填充数组。我建议您阅读 文档HTTP::Request::Common,POST 部分特别适用于更简洁的方法。
The problem is that for some reason you've enclosed your form values in single quotes. You want to send the data structure. E.g.:
You want to either send the hash reference of
%form, not the has reference contained within an array reference as you had (
[ \%form ]). If you had wanted to send the data as an array reference, then you'd just use
[ %form ]` which populates the array with the key/value pairs from the hash.I'd suggest that you read the documentation for HTTP::Request::Common, the POST section in particular for a cleaner way of doing this.