How to get valid JSON response from axios library using nodeJS

别说谁变了你拦得住时间么 提交于 2021-01-29 14:22:44

问题


I'm trying to hit the REST endpoint by using AXIOS library and the response.data return the below in console.log:

Reseponse for 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:

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()

Getting below response in console.log when I tried to use JSON.stringify(response.data):

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);
           });

回答1:


You DO get the right data, just node.js doesn't display it in the console/stdout. You can use util.inspect() for a better formatted output. Try this:

const util = require('util');
// ...
console.log(util.inspect(response.data, { showHidden: false, depth: null }));


来源:https://stackoverflow.com/questions/63621959/how-to-get-valid-json-response-from-axios-library-using-nodejs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!