0

Im new to node.js and consuming apis, I'm trying to access the returned 'access_token' but when I do res.access_token I get an undefined...any idea what I should be doing?

My node.js file

const https = require("https");

const options = {
    method: 'POST',
    host : 'api.com',
    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()

Which returns the following

statusCode: 200
{"access_token":"********","token_type":"Bearer","expires_in":7199}
CatGirl19
  • 209
  • 6
  • 18

1 Answers1

0

That in that snippet, you have a callback function which has a res parameter. That is the Response object. What you are looking for is called the response body. There is a similar question that has a good answer which will likely provide the info you need. Get body from the data event

jheagle
  • 13
  • 4