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

编程入门 行业动态 更新时间:2024-10-07 00:22:16

从node.js中返回的响应钻取API<a href=https://www.elefans.com/category/jswz/34/1771401.html style=元素"/>

从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元素

本文发布于:2024-05-07 05:17:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1754459.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   node   js   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!