最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

从node.js中返回的响应钻取API元素

网站源码admin18浏览0评论

从node.js中返回的响应钻取API元素

从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

发布评论

评论列表(0)

  1. 暂无评论