2014-01-17 66 views
0

我有一個node.js http.request調用的問題。我想調用外部API(在我的情況下,Openstack API)。請求發送成功,我得到的答覆,但我的請求沒有收到答覆後完成。而是等待大約2分鐘,然後請求結束。當從Postman這樣的REST客戶端執行相同的REST調用時,不需要等待,一切運行良好。Node.js http請求沒有結束

這是我的代碼,當我在/ auth URL上進行GET請求時調用該函數。

var options = { 
    host: 'someIp', 
    path: '/auth', 
    method: 'GET', 
    headers: { 
     "Host":"auth.api.someIp", 
     "X-Auth-User":"username", 
     "X-Auth-Key":"key" 
    } 
}; 

var request = http.request(options, function(response){ 
    var body = "" 
    response.on('data', function(data) { 
     body += data; 
    }); 
    response.on('end', function() {    
     console.log('response end') 
    }); 
}); 
request.on('error', function(e) { 
    console.log('Problem with request: ' + e.message); 
}); 
request.on('end', function() { 
    console.log('request end'); 
}); 

request.end(); 

這是控制檯日誌:

響應結束 GET/AUTH 200 120080ms

我的問題是,爲什麼不要求當我和node.js的http.request做結束?

+0

例如向「google.com」發出請求,如果它有效,那麼可能是因爲您沒有指定或無效的標頭。 – micnic

+0

我已經嘗試了以上,但一切都是一樣的,仍然等待大約2分鐘... – Case09

回答

0

Mmh嘗試收聽完成事件。

request.on('finish', function() { 
    console.log('request end'); 
}); 

請求是Incoming Message一個實例。

+0

我試圖聽取關閉和完成事件。關閉時沒有變化,當我聽完成事件時,它將輸出寫入控制檯日誌,但在完成之前它仍然等待大約兩分鐘。 – Case09

+0

也許用你的用戶名/密碼? –

+0

我連接到的api正在給我返回一個有效的響應,所以它不能因爲用戶名/密鑰。所以我得到了所有需要的數據,但請求仍然沒有結束。 – Case09