axios 不发送 HTTP post 请求
Axios does not send HTTP post request
我在使用 axios
的 nodejs 中的 alexa 技能中执行 http post 请求时遇到问题
我在这个项目之前使用过 axios,发送 CRUD 请求从来没有遇到过任何问题。
我的请求处理程序如下所示:
const handlers = {
'LaunchRequest': function () {
this.emit(':ask', 'What is your emergency?', 'How can I help you' )
},
'InjuryHelpIntent': function () {
const accessToken = this.event.context.System.user.accessToken
const userId= this.event.context.System.user.userId
console.log('user id: ', userId)
getDeviceAddress(this.event)
.then((address) => {
const res = sendHelp(address,accessToken)
console.log(res)
this.emit(':tell', 'Succes!')
})
.catch((error) => {
console.log('Error message: ',error)
this.emit(':tell', error)
})
},
}
在 sendHelp(address, token)
函数中,我调用了 REST 服务。
SendHelp.js:
const axios = require('axios')
module.exports = (address, token) => {
axios.post('https://api-sandbox.safetrek.io/v1/alarms')
.then(response => {
console.log(response)
return response})
.catch(error => {
console.log(error)
return error})
}
与此同时,我曾尝试 post 数据,但没有任何效果,即使是未经授权的调用,就像您在 sendHelp.js
中看到的我绝望的尝试一样。
由于缺少授权,我预计会出现 401 错误。 const res
在我的处理程序中应该是一个 json 对象,但我得到的是 undefined
。它完全跳过 POST 请求。
您不能 return 来自像 axios.post()
这样的异步函数的值并期望简单地同步接收 returned 值。换句话说,这是行不通的:
const res = sendHelp(address,accessToken)
有两个原因。首先 sendHelp()
实际上 return 什么都没有。即使它做到了,也需要 return 一个承诺,而不是 async axios 函数的结果。您需要 return 来自 axios 的承诺,然后对其调用 then()
。例如:
const axios = require('axios')
module.exports = (address, token) => {
// axios.post() returns a promise. Return that promise to the caller
return axios.post('https://api-sandbox.safetrek.io/v1/alarms')
.then(response => {
console.log(response)
return response
})
.catch(error => {
console.log(error)
return error
})
}
现在您可以使用该承诺,例如:
getDeviceAddress(this.event)
.then((address) => sendHelp(address,accessToken))
.then(res => {
console.log(res)
this.emit(':tell', 'Succes!')
})
.catch(err => handleError())
我在使用 axios
的 nodejs 中的 alexa 技能中执行 http post 请求时遇到问题我在这个项目之前使用过 axios,发送 CRUD 请求从来没有遇到过任何问题。
我的请求处理程序如下所示:
const handlers = {
'LaunchRequest': function () {
this.emit(':ask', 'What is your emergency?', 'How can I help you' )
},
'InjuryHelpIntent': function () {
const accessToken = this.event.context.System.user.accessToken
const userId= this.event.context.System.user.userId
console.log('user id: ', userId)
getDeviceAddress(this.event)
.then((address) => {
const res = sendHelp(address,accessToken)
console.log(res)
this.emit(':tell', 'Succes!')
})
.catch((error) => {
console.log('Error message: ',error)
this.emit(':tell', error)
})
},
}
在 sendHelp(address, token)
函数中,我调用了 REST 服务。
SendHelp.js:
const axios = require('axios')
module.exports = (address, token) => {
axios.post('https://api-sandbox.safetrek.io/v1/alarms')
.then(response => {
console.log(response)
return response})
.catch(error => {
console.log(error)
return error})
}
与此同时,我曾尝试 post 数据,但没有任何效果,即使是未经授权的调用,就像您在 sendHelp.js
中看到的我绝望的尝试一样。
由于缺少授权,我预计会出现 401 错误。 const res
在我的处理程序中应该是一个 json 对象,但我得到的是 undefined
。它完全跳过 POST 请求。
您不能 return 来自像 axios.post()
这样的异步函数的值并期望简单地同步接收 returned 值。换句话说,这是行不通的:
const res = sendHelp(address,accessToken)
有两个原因。首先 sendHelp()
实际上 return 什么都没有。即使它做到了,也需要 return 一个承诺,而不是 async axios 函数的结果。您需要 return 来自 axios 的承诺,然后对其调用 then()
。例如:
const axios = require('axios')
module.exports = (address, token) => {
// axios.post() returns a promise. Return that promise to the caller
return axios.post('https://api-sandbox.safetrek.io/v1/alarms')
.then(response => {
console.log(response)
return response
})
.catch(error => {
console.log(error)
return error
})
}
现在您可以使用该承诺,例如:
getDeviceAddress(this.event)
.then((address) => sendHelp(address,accessToken))
.then(res => {
console.log(res)
this.emit(':tell', 'Succes!')
})
.catch(err => handleError())