-1

所以,我正在尝试向 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,
            "length": 11,
            "weight": 0.3,
            "insurance_value": 55.05,
            "quantity": 2
        },
        {
            "id": "z",
            "width": 22,
            "height": 30,
            "length": 11,
            "weight": 1,
            "insurance_value": 30,
            "quantity": 1
        }
    ]
}
4

1 回答 1

0

执行以下操作,

  • 验证process.env.ENDPOINT是否正确
  • 接受工作请求postman并转到code生成代码片段并选择nodejs - axios并与您必须查看的代码进行比较以查看差异在哪里。 生成代码片段 - nodejs - axios

这篇描述了什么422意思的帖子,在这种情况下,它指向一些导致错误的输入。

于 2021-07-18T07:00:06.367 回答