admin管理员组文章数量:1393302
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
1 Answer
Reset to default 3You 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 statusText
property 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.
本文标签: javascriptHow to retrieve detail of HTTP response errorStack Overflow
版权声明:本文标题:javascript - How to retrieve detail of HTTP response error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744643733a2617272.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论