元素"/>
从node.js中返回的响应钻取API元素
我是node.js的新用户,正在使用api,我正在尝试访问返回的'access_token'
,但是当我执行res.access_token
时,我得到的是未定义的...知道我应该做什么吗?
我的node.js文件
const https = require("https");
const options = {
method: 'POST',
host : 'api',
path: '/oauth2/token'
+'?grant_type=client_credentials'
+'&client_id=*******'
+'&client_secret=***********',
headers:
{
'cache-control': 'no-cache',
Flow: 'application',
}
};
const req = https.request(options, res => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', d => {
process.stdout.write(d)
})
})
req.on('error', error => {
console.error(error)
})
req.end()
返回以下内容
statusCode: 200
{"access_token":"********","token_type":"Bearer","expires_in":7199}
回答如下:在该代码段中,您有一个带有res参数的回调函数。那是Response对象。您正在寻找的被称为响应主体。有一个类似的问题,它的回答很好,很可能会提供您所需的信息。Get body from the data event
更多推荐
从node.js中返回的响应钻取API元素
发布评论