问题描述
所以,我正在尝试向 api 发出请求,当我在节点项目上使用 Axios 执行此操作时,它返回 422,当我执行相同的请求时,使用相同的标头和相同的数据,在邮递员或雷霆客户端,他们给了我 200 和我需要的数据。我遇到了这条特定路线的问题。
const config = () => {
return {
headers: {
Authorization: `Bearer ${process.env.TOKEN}`,Accept: 'application/json','User-Agent': `Aplicação (${process.env.CONTACT_EMAIL})`
}
}
}
router.post('/calculate',async (req,res) => {
const resp = await Axios.post(`${process.env.ENDPOINT}/api/v2/me/shipment/calculate`,req.body,config())
return res.status(200).send(resp.data);
})
请求体:
{
"from": {
"postal_code": "96020360"
},"to": {
"postal_code": "01018020"
},"products": [
{
"id": "x","width": 11,"height": 17,"length": 11,"weight": 0.3,"insurance_value": 10.1,"quantity": 1
},{
"id": "y","width": 16,"height": 25,"insurance_value": 55.05,"quantity": 2
},{
"id": "z","width": 22,"height": 30,"weight": 1,"insurance_value": 30,"quantity": 1
}
]
}
解决方法
执行以下操作,
- 验证
process.env.ENDPOINT
是否正确 - 获取
postman
中的工作请求并转到code
以生成代码片段并选择nodejs - axios
并与您要查看的代码进行比较以查看差异所在。
This post describing what 422
means,在这种情况下,它指向一些导致错误的输入。