最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to retrieve detail of HTTP response error - Stack Overflow

programmeradmin2浏览0评论

I've got a node.js application that is making some https requests to a ReST web service. I want to do something that, on the face of it, appears like it should be simple - retrieve the error message that es back from the web service.

I can get hold of the status code - i.e. 200, 404 etc but not the detail of the error.

The body of the response looks like this:

{
    "type": ".html#sec10.4.5",
    "title" : "Not Found",
    "status": "404",
    "detail": "Resource not found: X33003"
}

My code looks like this:

var options = {
    "method": "POST",
    "hostname": "myhost",
    "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);
}

It would be useful to be able to present what exactly went wrong - i.e the message: Resource not found: X33003 from the JSON above. How can I get hold of that?

I've got a node.js application that is making some https requests to a ReST web service. I want to do something that, on the face of it, appears like it should be simple - retrieve the error message that es back from the web service.

I can get hold of the status code - i.e. 200, 404 etc but not the detail of the error.

The body of the response looks like this:

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

My code looks like this:

var options = {
    "method": "POST",
    "hostname": "myhost.",
    "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);
}

It would be useful to be able to present what exactly went wrong - i.e the message: Resource not found: X33003 from the JSON above. How can I get hold of that?

Share Improve this question edited Aug 23, 2017 at 5:26 Ross Coundon asked Aug 22, 2017 at 21:03 Ross CoundonRoss Coundon 9271 gold badge12 silver badges20 bronze badges 2
  • Can we see the options object? Also, are you expecting binary data? – ishegg Commented Aug 22, 2017 at 21:18
  • Hi ishegg - Updated to include the Options object. The data returned isn't binary, there's either nothing returned with status of 200 or, I think, a body containing the JSON I included. – Ross Coundon Commented Aug 23, 2017 at 5:28
Add a ment  | 

1 Answer 1

Reset to default 3

You just had the wrong properties of the objects you were calling. First, you were calling body.detail, but body was the Buffer representation. You need to call the detail property on response. Second, you were trying to get the statusTextproperty of the response, but the correct property is statusMessage. Code ends up like this:

var options = {
    "method": "POST",
    "hostname": "myhost.",
    "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);
}

It's always a good idea to console.log (or the equivalent) the object you're trying to access if you're not getting the correct results, that will show you all the properties of the object.

发布评论

评论列表(0)

  1. 暂无评论