-1

So, i'm trying to make a request to an api, when I do it with Axios on my node project, it returns me 422, and when I do the same request, with the same header, and the same data, on postman or thunder client, they give me 200 and the data that I need. I'm having problem with this route specific.

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);
})

req.body:

{
    "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
        }
    ]
}
Harry
  • 39
  • 4
  • What is `req.body`? Have you tried debugging that value? – Phil Jul 18 '21 at 03:31
  • Shouldn't you use header 'Content-Type' instead of 'Accept'? – Darshani Audhish Jul 18 '21 at 03:34
  • req.body is the data that I send as body of POST request Phil. I send Accept because the docs of this API request to. But, Postman use this same header and get the right response – Harry Jul 18 '21 at 03:37
  • @DarshaniAudhish the Axios header defaults are `{ "Content-type": "application/json" }` for POST requests – Phil Jul 18 '21 at 03:39
  • @Harry that doesn't answer my question at all. What data-type is `req.body`? What value does it contain? Please do some simple debugging. Even `console.log(typeof req.body, JSON.stringify(req.body))` – Phil Jul 18 '21 at 03:40
  • just updated with the req.body – Harry Jul 18 '21 at 06:05
  • Is that what you're posting _to_ your Express API (`/calculate`) or is that what you've logged from your debugging attempts? Have you added in the appropriate body processing middleware, ie `app.use(express.json())` – Phil Jul 18 '21 at 06:13

1 Answers1

0

Perform the following,

  • Verify that process.env.ENDPOINT is correct
  • take the working request in postman and go to code to generate code snippet and select nodejs - axios and compare with the code you have to see where the difference is. generate code snippet - nodejs - axios

This post describing what 422 means, in this situation, it points to some input that is causing an error.

Manny
  • 369
  • 2
  • 7