有一个网页,其中包含表单内的按钮。单击该按钮可以下载POST请求并下载CSV文件。
我尝试使用LWP :: UserAgent自动执行CSV下载过程。
我从Chrome开发者工具中注意到Content-Type
是multipart/form-data; boundary=---WebKitFormBoundary....
任何想法如何发送开发者工具显示的确切Request Payload
?
我通常会针对x-www-form-urlencoded
内容类型执行以下操作。但我不知道如何提交多部分表单数据。
my $ua = LWP::UserAgent->new;
$ua->cookie_jar({ file => "cookie.txt", autosave => 1});
my $request = HTTP::Request->new('POST', $url);
#copy the form_data from chrome developer tools
my $form_data = 'key=val&key2=val2';
#the form_data is too big (and is in parts) in case of multipart content type
$request->content($form_data);
$request->header('Content-Type' => "application/x-www-form-urlencoded");
#I'll have to use `multipart/form-data; boundary=---WebKitFormBoundary....`
#add some other headers like 'Origin', 'Host' and 'Referer' in similar manner
#...
#...
push @{ $ua->requests_redirectable }, 'POST';
my $response = $ua->request($request);
#Get the file
答案 0 :(得分:3)
查看the documentation for HTTP::Request::Common
POST
上的条目
您可以通过传递multipart/form-data
application/x-www-form-urlencoded
请求(而不是Content_Type => 'form-data'
)
看起来像这样
my $response = $ua->post(
$url,
Content_Type => 'form-data',
{
...; # Hash of form key/value pairs
}
);