2017-08-22 13 views
2

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。どのように私はそれを保持することができますか?

+0

'options'オブジェクトは見えますか?また、バイナリデータを期待していますか? – ishegg

+0

Hi ishegg - Optionsオブジェクトを含むように更新されました。返されるデータはバイナリではありません。ステータスが200のものは返されませんでした。 –

答えて

1

あなたは、呼び出したオブジェクトの間違った特性を持っていました。まず、body.detailに電話していましたが、bodyBufferでした。 responsedetailプロパティを呼び出す必要があります。次に、レスポンスの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の使用)を試しましたが、プロパティを表示するのに便利な方法が見つかりませんでした。私はちょうど[オブジェクト]が印刷されて参照してくださいそのプロパティを記録するようにオブジェクトを調べる方法がありますか? –

+0

'res'オブジェクトを' console.log() 'するとどうなるでしょうか?私はターミナルで完全な本物のオブジェクトを取得します。 – ishegg

+0

私はreqオブジェクトが必要な時に間違ってそれに取りつかれていました。私はresの完全な内容を見ることができます。再度ありがとう –

関連する問題