axios - Wizaplace API 401 Unauthorized but token worked in the previous request
问题描述
I have a 401 on a request I made on Wizaplace's natively generated API route: GET /api/v1/orders/{{orderId}}
(to get anorder using its ID), I'm passing the same token (apiKey) than in previous call to POST /api/v1/basket
(to empty the cart) so I don't understand why this is happening. Any help would be welcome !
Here's my code:
async function getOrderById (req, orderId) {
try {
console.log(req.header('x-wiza-token'))
const ret = axios.get(WIZAPLACE_URL + '/orders/' + orderId, {},
{
headers: {
Authorization: req.header('x-wiza-token')
}
}) // This is returning 401
console.log(ret.data)
return ret.data
} catch (e) {
// eslint-disable-next-line no-console
console.log('Error: ', e)
}
}
app.post('/basket/:id/order', async (req, res) => {
try {
console.log('[Order]') // eslint-disable-line
console.log('URL: ', WIZAPLACE_URL + '/basket/' + req.params.id + '/order') // eslint-disable-line
console.log('Auth headers: ', req.header('x-wiza-token')) // eslint-disable-line
console.log('body: ', req.body) // eslint-disable-line
const ret = await axios.post(WIZAPLACE_URL + '/basket/' + req.params.id + '/order',
req.body,
{
headers: {
Authorization: req.header('x-wiza-token')
}
})
if (ret.data.orders && !ret.data.html) {
// This should reset a cart only when and order was successfull
axios.post(WIZAPLACE_URL + '/basket', {}, {
headers: {
Authorization: req.header('x-wiza-token')
}
}) // This call is working
}
console.log('Order ret: ', ret.data) // eslint-disable-line
const orders = []
for (let i = 0; i !== ret.data.orders.length; i++) {
console.log(ret.data.orders[i])
orders.push(getOrderById(req, ret.data.orders[i].id))
}
console.log(orders)
res.send(ret.data) // This is working too
} catch (e) {
// eslint-disable-next-line no-console
console.log('Error: ', e.response)
res.status(400).send(e)
}
})
I really don't know what I'm missing here. Any help on this would be super welcome !
Here's a screenshot of the documentation:
解决方案
推荐阅读
- java - Java - 返回一个新数组,其中元素位于给定数组的偶数位置
- laravel - 如何在 Laravel 应用程序中出于不同目的两次“使用 Spotify 登录”?
- r - 在 Shiny 中添加和删除元素
- java - 无法在 Maven 或 Eclipse 中构建 ehealth-connector
- python - 循环使用python查找空间
- javascript - 如何检查图像是否在请求 Node.js 中
- spring-mybatis - Mybatis的mapper接口与对应的Mapper xml文件绑定的方式是什么
- javascript - 为什么我生成的所有按钮都会链接到数组中的最后一项?
- multithreading - 控制 OS 线程与 goroutine 的启动?
- python - docker: 构建 cuda 映像太大的 opencv (22.4 GB)