[英]How to get valid JSON response from axios library using nodeJS
I'm trying to hit the REST endpoint by using AXIOS library and the response.data return the below in console.log:我正在尝试使用 AXIOS 库访问 REST 端点,并且 response.data 在 console.log 中返回以下内容:
Reseponse for console.log(response.data)对 console.log(response.data) 的响应
{
sqlQuery: "select type,subtype from wetrade_p2 where parent_id='69341269'",
message: '1 rows selected',
row: [ { column: [Array] } ]
}
But when I hit the same REST endpoint in postman and I can get the entire Response JSON as like below:但是当我在邮递员中点击相同的 REST 端点时,我可以获得整个响应 JSON,如下所示:
PostMan Output (Expected):邮递员输出(预期):
{
"sqlQuery": "select type,subtype from wetrade_p2 where parent_id='69341269'",
"message": "2 rows selected",
"row": [
{
"column": [
{
"value": "W",
"name": "TYPE"
},
{
"value": "P",
"name": "STATUS"
},
{
"value": "0",
"name": "SUBTYPE"
},
{
"value": "USD",
"name": "CURRENCY"
}
]
},
{
"column": [
{
"value": "W",
"name": "TYPE"
},
{
"value": "S",
"name": "STATUS"
},
{
"value": "0",
"name": "SUBTYPE"
},
{
"value": "USD",
"name": "CURRENCY"
}
]
}
]
}
I also tried to stingify the response.data and it returned below response which is not able to parse()我还尝试对 response.data 进行 stingify,它返回了无法解析的响应()
Getting below response in console.log when I tried to use JSON.stringify(response.data):当我尝试使用 JSON.stringify(response.data) 时,在 console.log 中得到以下响应:
sqlQuery: "select type,subtype from wetrade_p2 where parent_id=69341269"
message: "2 rows selected"
row: [
{
"column": [
{
"value": "W",
"name": "TYPE"
},
{
"value": "P",
"name": "STATUS"
},
{
"value": "0",
"name": "SUBTYPE"
},
{
"value": "USD",
"name": "CURRENCY"
}
]
},
{
"column": [
{
"value": "W",
"name": "TYPE"
},
{
"value": "S",
"name": "STATUS"
},
{
"value": "0",
"name": "SUBTYPE"
},
{
"value": "USD",
"name": "CURRENCY"
}
]
}
]
Sample Code:示例代码:
await axios[methodType](url, body, {
httpsAgent:httpsAgent,
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Access-Control-Allow-Origin": true
}
}).then(response => {
console.log("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
console.log(response.data);
console.log(JSON.stringify(response.data))
console.log("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
console.log(response);
}).catch(error => {
console.log(error);
});
You DO get the right data, just node.js doesn't display it in the console/stdout.你确实得到了正确的数据,只是 node.js 没有在控制台/标准输出中显示它。 You can use
util.inspect()
for a better formatted output.您可以使用
util.inspect()
以获得更好的格式化输出。 Try this:尝试这个:
const util = require('util');
// ...
console.log(util.inspect(response.data, { showHidden: false, depth: null }));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.