2013-11-28 20 views
0

編寫perl腳本以在單個會話上發送多個帖子請求。所以每當我發送第一個請求時,它都會返回一些會話ID。我必須使用相同的會話ID執行下一個請求。如何在http頭中包含該會話。 我不想使用任何http:cookie標頭。我可以直接在http頭中傳遞會話ID嗎? ?wirte perl腳本,用於具有先前資源的http post請求

示例代碼(不工作

$server_endpoint = "https://ip/nitro/v1/discovery/device_registration"; 
$post_data = '{"device_registration":{"device_ipaddress":"ip","device_family":"CB"}}'; 
$req = HTTP::Request->new(POST => $server_endpoint); 
$req->header('content-type' => 'application/json'); 
$req->header('cookie' => 'sessionid=$sessionid;'); # can I avoid setting a cookie? 
$req->content($post_data); 

回答

1

這取決於服務是如何期望接收的會話ID。你將不得不從API文檔中找到它。

如果該服務被期待的SessionID作爲POST數據的一部分,那麼你可以保留POST數據,便於更新哈希,而當它的時間來發出請求只序列化:

use JSON; 

$server_endpoint = "https://ip/nitro/v1/discovery/device_registration"; 
$post_data = { 
    device_registration => { 
     device_ipaddress => 'ip', 
     device_family => 'CB', 
    } 
}; 

$req = HTTP::Request->new(POST => $server_endpoint); 
$req->header('Content-Type' => 'application/json'); 

$post_data->{sessionid} = $sessionid; 
$req->content(JSON::to_json($post_data)); 

但是,這是否適用於您的特定情況取決於服務如何期待交付sessionid。

+0

所以,如果我想看看它是如何從用戶界面,我該如何檢查那一個? – Kranthi

+0

就像我說的,你必須檢查API文檔。這是你正在寫的服務嗎? http://support.citrix.com/servlet/KbServlet/download/30602-102-681756/NS-Nitro-Gettingstarted-guide.pdf該文檔建議認證令牌確實必須作爲HTTP cookie進行傳遞。 –