【问题标题】:How to retrieve detail of HTTP response error如何检索 HTTP 响应错误的详细信息
【发布时间】:2026-01-26 17:10:01
【问题描述】:

我有一个 node.js 应用程序正在向 ReST Web 服务发出一些 https 请求。 我想做一些从表面上看应该很简单的事情 - 检索从 Web 服务返回的错误消息。

我可以获取状态码 - 即 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 - 更新为包含选项对象。返回的数据不是二进制的,要么没有返回任何状态为 200 的数据,要么我认为是包含我包含的 JSON 的正文。

标签: javascript node.js httprequest


【解决方案1】:

您调用的对象的属性有误。首先,您调用的是body.detail,但bodyBuffer 的表示。您需要调用response 上的detail 属性。其次,您试图获取响应的statusTextproperty,但正确的属性是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(或等效的),这将显示该对象的所有属性。

【讨论】:

  • 谢谢 - 我会试一试。在日志记录方面,绝对 - 我一直想这样做。我已经尝试过记录(控制台和使用 log4js),但还没有找到一种有用的方法来显示属性。我只看到 [object] 打印出来了。你有办法询问对象以便记录其属性吗?
  • 当你console.log() res 对象时你会得到什么?我在终端中得到了完整的对象。
  • 当我需要 req 对象时,我错误地痴迷于它。我可以看到 res 的全部内容。再次感谢
  • 太棒了!乐于助人。
最近更新 更多