2017-08-22 30 views
2

我有一個node.js應用程序正在向ReST Web服務發出一些https請求。 我想做一些事情,就它而言,看起來應該很簡單 - 檢索從Web服務返回的錯誤消息。如何檢索HTTP響應錯誤的詳細信息

我可以得到狀態碼 - 即200,404等,但不是錯誤的細節。

響應的身體看起來是這樣的:

{ 
    "type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5", 
    "title" : "Not Found", 
    "status": "404", 
    "detail": "Resource not found: X33003" 
} 

我的代碼如下所示:

var options = { 
    "method": "POST", 
    "hostname": "myhost.com", 
    "port": null, 
    "path": "/mypath/", 
    "headers": { 
     "content-type": "application/json", 
     "authorization": basicAuthString, 
     "cache-control": "no-cache" 
    } 
}; 

try { 
    var reqWorkSkill = http.request(options, function(res) { 
    var chunks = []; 
    res.on("data", function(chunk) { 
     chunks.push(chunk); 
    }); 
    res.on("end", function() { 
     var body = Buffer.concat(chunks);    
     var response = JSON.parse(body); 
     console.log("Detail: " + body.detail); // COMES BACK UNDEFINED 
    }); 
    res.on("error", function(error) {   
     console.log("Something went wrong with: " + resourceIdArray[i] + " failed: " + error); 
    }); 
    if(res.statusCode != 200){ 
     // Do some stuff 
    } 
    console.log("res status: " + res.statusCode); 
    console.log("res text: " + res.statusText); // COMES BACK UNDEFINED 

    }); 
    reqWorkSkill.write(itemToPost);      
    reqWorkSkill.end(); 
} 
catch (e) { 
    console.log(e); 
} 

這將是能夠提出究竟是什麼出了問題有用的 - 即消息:未找到資源:上面的JSON中的X33003。我怎麼能得到這個?

+0

我們可以看到'options'對象嗎?另外,你是否期待二進制數據? – ishegg

+0

Hi ishegg - 已更新爲包含Options對象。返回的數據不是二進制的,沒有任何狀態返回200,或者我認爲,包含我包含的JSON的主體。 –

回答

1

您剛纔調用的對象屬性錯誤。首先,你打電話body.detail,但bodyBuffer表示。您需要撥打response上的detail屬性。其次,您試圖獲得響應的statusText屬性,但正確的屬性是statusMessage。代碼最終是這樣的:

var options = { 
    "method": "POST", 
    "hostname": "myhost.com", 
    "port": null, 
    "path": "/mypath/", 
    "headers": { 
     "content-type": "application/json", 
     "authorization": basicAuthString, 
     "cache-control": "no-cache" 
    } 
}; 

try { 
    var reqWorkSkill = http.request(options, function(res) { 
    var chunks = []; 
    res.on("data", function(chunk) { 
     chunks.push(chunk); 
    }); 
    res.on("end", function() { 
     var body = Buffer.concat(chunks);    
     var response = JSON.parse(body); 
     console.log("Detail: " + response.detail); // response, not body 
    }); 
    res.on("error", function(error) {   
     console.log("Something went wrong with: " + resourceIdArray[i] + " failed: " + error); 
    }); 
    if(res.statusCode != 200){ 
     // Do some stuff 
    } 
    console.log("res status: " + res.statusCode); 
    console.log("res text: " + res.statusMessage); // statusMessage, not statusText 

    }); 
    reqWorkSkill.write(itemToPost);      
    reqWorkSkill.end(); 
} 
catch (e) { 
    console.log(e); 
} 

它總是一個好主意,console.log(或等值)你試圖訪問,如果你沒有得到正確的結果對象,會告訴你所有屬性的對象。

+0

謝謝 - 我會放棄。在伐木方面,絕對 - 我一直想要做到這一點。 我試過記錄(控制檯和使用log4js),但沒有找到一個有用的方式來獲取屬性顯示。我只看到[object]被打印。你有辦法查詢對象,以便記錄它的屬性嗎? –

+0

當你使用console.log()'res'對象時,你會得到什麼?我在終端中獲得了完整的物體。 – ishegg

+0

當我需要res時,我對關於req對象的錯誤置若罔聞。我可以看到水庫的全部內容。再次感謝 –