如何从process.stdout.write(d)中获取d值



我有存储访问令牌的"d",如果我使用console.log(d(,我如何从他那里获得这个值:

<Buffer 7b 22 61 63 63 65 73 73 5f 74 6f 6b 65 6e 22 3a 22 65 61 38 30 66 32 30 35 38 38 32 37 34 64 37 32 62 61 64 66 30 36 31 37 64 36 37 62 36 65 34 38 22 ... 362 more bytes> 

我试过使用toString(),str='',str+=d,但无法获得

const auth=()=>{
const data=querystring.stringify({
grant_type: 'client_credentials',
client_id: 'x',
client_secret: 'x',
})
var options = {
port:443,
host: 'rest.sandbox.eu.zuora.com',
path:'/oauth/token',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': data.length
},
method: 'POST',
};
const req=https.request(options,(res)=>{

res.on('data', (d) => {
process.stdout.write(d)
}).on('end',()=>{
console.log("Yay");
});
})
req.write(data)
req.end()
}
auth()

尝试类似的东西(未测试(

const auth=()=>{
const data=querystring.stringify({
grant_type: 'client_credentials',
client_id: 'x',
client_secret: 'x',
})
var options = {
port:443,
host: 'rest.sandbox.eu.zuora.com',
path:'/oauth/token',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': data.length
},
method: 'POST',
};
const req=https.request(options,(res)=>{

let body = []
res.on('data', (d) => {
body.push(d)
}).on('end',()=>{
try {
console.log(JSON.parse(Buffer.concat(body).toString()));
} catch(e) {
console.log('some error')
}
});
})
req.write(data)
req.end()
}
auth()

我不确定你是如何使用toString的,但如果我调用toString(),作为

theString = d.toString()

它对我有用。

最新更新