在Perl中发布文件时出错

时间:2014-08-16 16:47:53

标签: forms perl post request

我试图重建一个请求(文件上传),来自webbrowser的打包发送如下所示:

-----------------------------596088922920
Content-Disposition: form-data; name="Filedata"; filename="mypic.gif"
Content-Type: image/gif

GIF87aX#  *shorted*  ¡§\
-----------------------------596088922920
Content-Disposition: form-data; name="session"

92384729384792384729384729384
-----------------------------596088922920
Content-Disposition: form-data; name="method"

put
-----------------------------596088922920--

我的每个代码看起来像这样:

use WWW::Mechanize;
$agent = WWW::Mechanize->new( cookie_jar => {} );
  $agent->post('http://www.kleiderkreisel.de/photo/ajax_photo_action',
    [
        "Filedata" => ['mypic.gif' => 'C:\Pics\mypic.gif'],
        'session' => '92384729384792384729384729384',
        'method' => 'put'
    ]);

遗憾的是,这给了我错误“内部服务器错误”... 我做错了什么?

1 个答案:

答案 0 :(得分:4)

根据HTTP::Request::Common(从LWP::UserAgent->post引用的WWW::Mechanize->post引用)的文档,您必须明确将内容类型设置为form-data。因此代码应该是:

$agent->post('http://www.kleiderkreisel.de/photo/ajax_photo_action',
    Content_Type => 'form-data',
    Content =>  [
        "Filedata" => ['mypic.gif' => 'C:\Pics\mypic.gif'],
        'session' => '92384729384792384729384729384',
        'method' => 'put'
    ]
);