0

我在创建可以使用 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 变量时,没有设置任何内容:(

4

1 回答 1

0

问题是由于某种原因,您将表单值括在单引号中。您要发送数据结构。例如:

$resp = $ua->post('http://someurl/test.php', 
                  'Content_Type' => 'multipart/form-data',
                  'Content'      => \%form);

您想发送%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 ]` 的哈希引用,它使用哈希中的键/值对填充数组。

我建议您阅读HTTP::Request::Common 的文档,特别是 POST 部分,以获得更简洁的方法。

于 2011-06-30T14:37:40.117 回答