【问题标题】:How to post array form using LWP如何使用 LWP 发布数组形式
【发布时间】:2011-06-30 14:01:42
【问题描述】:

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

【问题讨论】:

    标签: perl lwp


    【解决方案1】:

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

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

    我建议您阅读 documentation for HTTP::Request::Common,特别是 POST 部分,以便更简洁地执行此操作。

    【讨论】:

    • No dice :( 我已经转储数据以检查数组,看起来没问题。{ Action => "check", Submit => 1, file_1 => ["./test.txt" ] } 我尝试过使用 $ua->request(POST $url, %form); 如果我使用 $ua->request(POST $url, { 'Action' => 'test' }); 来简化测试,那就行了很好。
    • @UxBoD - 您正在使用文件名的相对路径,如果您提供文件的完整路径,它是否有效? '/home/uxbod/test.txt' 而不是 './test.txt'
    • Nit: -&gt;post 只是将它的参数传递给POST,所以后者并不干净,它是一样的。
    猜你喜欢
    • 2013-12-16
    • 2011-04-19
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-31
    相关资源
    最近更新 更多