Node.js 如何从axios post API请求中获取特定字段
我正在使用下面的代码Node.js 如何从axios post API请求中获取特定字段,node.js,axios,mocha.js,Node.js,Axios,Mocha.js,我正在使用下面的代码 const axios = require('axios') axios .post('https://xxx', { "audience": "http://xxxx", "grant_type": "xxxxx", "client_id": "xxxxx", "client_secret": &
const axios = require('axios')
axios
.post('https://xxx', {
"audience": "http://xxxx",
"grant_type": "xxxxx",
"client_id": "xxxxx",
"client_secret": "xxxxx"
})
.then(res => {
console.log(res)
})
.catch(error => {
console.error(error)
})
我想将“res.data.token”分配给变量token,并在下面的代码中使用该变量
describe('/GET device information', function () {
it("it should GET a Good Auth Status", function(done) {
chai.request('http:xxxxxx')
.get('xxxxxxxxxxxx')
.set({ "Authorization": `Bearer ${token}` })
.then((res) => {
(res).should.have.status(200);
// console.log(body) - not really needed, but I include them as a comment
done();
}).catch((err) => done(err))
});
})
您可以将其包装在try/catch中,然后对对象进行解构:
试试看{
const res=等待axios.post('https://xxx', {
‘观众’http://xxxx',
“授权类型”:“xxxxx”,
“客户id”:“xxxxx”,
“客户机密”:“xxxxx”
})
const{data,token,foo,bar,status}=res.data
(状态)。应等于(200)
}捕获(e){
控制台日志(e)
}
}
快速示例您可以将其包装在try/catch中,然后对对象进行分解:
试试看{
const res=等待axios.post('https://xxx', {
‘观众’http://xxxx',
“授权类型”:“xxxxx”,
“客户id”:“xxxxx”,
“客户机密”:“xxxxx”
})
const{data,token,foo,bar,status}=res.data
(状态)。应等于(200)
}捕获(e){
控制台日志(e)
}
}
快速示例