简体   繁体   English

如何解决“NodeJS API 调用响应是不可解析的对象”?

[英]how to resolve 'NodeJS API call response is an un-parsable object'?

I am getting the below result after my API call.在调用 API 后,我得到了以下结果。

My node version is 12.x我的节点版本是 12.x

 {"type":"Buffer","data":[123,34,101,114,114,111,114,115,34,58,91,34,74,87,84,32,105,115,32,101,120,112,105,114,101,100,32,111,114,32,100,111,101,115,32,110,111,116,32,104,97,118,101,32,112,114,111,112,101,114,32,39,101,120,112,39,32,99,108,97,105,109,34,93,125,11]}

Please see the code snippet below:请看下面的代码片段:

let postOptions = {
        host: 'vault.server',
        path: '/v1/auth/gcp/login',
        method: HTTPS.POST_REQUEST,
        headers: {
            'Content-Type': 'application/json; charset=utf-8',
            'X-Vault-Namespace': 'mynamespace'
        },
        json: true,
        
        rpDefaults: {
            strictSSL: false
        }
    };

    let requestPayLoad = {

        "role": this._vaultRole,
        "jwt": signedJWT
    };
    console.log(JSON.stringify(requestPayLoad));
    console.log(JSON.stringify(postOptions));
    try {
        let result = await HTTPS.makeRequest(postOptions, JSON.stringify(requestPayLoad), HTTPS.POST_REQUEST);
        console.log('Response***************',JSON.stringify(result));
        return result.auth.client_token;
    }

Please see the below code snippet for the http make request method.请参阅以下代码片段了解 http 发出请求方法。

return new Promise((resolve, reject) => {
        let rq = https.request(options, (res) => {
            let response;
            let chunks = [];
            res.on('data', (chunk) => {
                chunks.push(chunk);
            });
            res.on('end', () => {
                response = Buffer.concat(chunks);
                return resolve(response);
            });
        });
        rq.on('error', (e) => {
            return reject({'statusCode': 500, 'success': false, 'error': e.toString()});
        });
        if (type === 'POST') {
            rq.write(data);
        }
        rq.end();
    });

Please help me to resolve this请帮我解决这个问题

You are receiving the data as a Buffer.您将数据作为缓冲区接收。 Use the toString() method to convert this buffer into a string inside the try block.使用toString()方法将此缓冲区转换为try块内的字符串。

 try { let result = await HTTPS.makeRequest(postOptions, JSON.stringify(requestPayLoad), HTTPS.POST_REQUEST); console.log('Response***************', result.toString()); return result.auth.client_token; }

If you want to access the data from the response returned from you API call do:如果您想从 API 调用返回的响应中访问数据,请执行以下操作:


let data = result.data;

and I you want to get client_token as showing here:我你想得到client_token,如下所示:

return result.auth.client_token;

it's not possible because the response does not have auth attribute on it:这是不可能的,因为响应上没有 auth 属性:

 {"type":"Buffer","data":[123,34,101,114,114,111,114,115,34,58,91,34,74,87,84,32,105,115,32,101,120,112,105,114,101,100,32,111,114,32,100,111,101,115,32,110,111,116,32,104,97,118,101,32,112,114,111,112,101,114,32,39,101,120,112,39,32,99,108,97,105,109,34,93,125,11]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM