2014-02-28 92 views
3

Perl的HTTP POST請求我需要發送與下列HTTP頭的HTTP POST請求:與內容類型和授權令牌

Content-type: 'application/atom+xml' 
Authorization: MyLogin auth=$token 

的令牌從一個授權子例程來。這裏是Perl在進行實際請求後,子程序是成功的:在運行時

my $ua = LWP::UserAgent->new; 
my $req = $ua->post ($url); 
    $req = header('Content-type' => 'application/atom+xml'); 
    $req = header('Authorization' => "MyLogin auth=$token"); 

不過,我收到以下錯誤:

Undefined subroutine &main::header called ... 

我怎樣才能做到這一點?

回答

3

按照LWP::UserAgent documentation,您可以通過將它們作爲參數傳入post設置附加標題:

my $ua = LWP::UserAgent->new; 
my $response = $ua->post($url, 
    'Content-type' => 'application/atom+xml', 
    'Authorization' => "MyLogin auth=$token" 
); 

注意$ua->post實際發送請求,所以試圖設置標題後調用它,如你在你的示例代碼中做的,是沒用的。 (更不用說main命名空間中沒有header函數,除非您從某處導入或自己寫入它。)