2017-05-04 139 views
0

有一個網頁,其包括形式內部的按鈕。單擊該按鈕可以使一個POST請求和CSV文件被下載。POST請求來下載文件

我試圖使用自動化LWP :: UserAgent的的CSV下載過程。

我從Chrome開發人員工具注意到Content-Typemultipart/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 

回答

2

採取the documentation for HTTP::Request::Common

看看上POST的條目您可以通過傳遞Content_Type => 'form-data'

僞報頭值它看起來像這樣

產生 multipart/form-data請求(而不是 application/x-www-form-urlencoded
my $response = $ua->post(
    $url, 
    Content_Type => 'form-data', 
    { 
     ...; # Hash of form key/value pairs 
    } 
);