ReST Webサービスへのhttps要求を行っているnode.jsアプリケーションがあります。 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。どのように私はそれを保持することができますか?
'options'オブジェクトは見えますか?また、バイナリデータを期待していますか? – ishegg
Hi ishegg - Optionsオブジェクトを含むように更新されました。返されるデータはバイナリではありません。ステータスが200のものは返されませんでした。 –