Unable to get Axios "POST" data filed

I am making an Axios "POST" request using front end:

  const res = await axios({
    method: "POST",
    url: `http://127.0.0.1:8000/api/v1/chatGPT`,
    body: {
      chat: `hello how are you ?`,
    },
  });

I have a Node.js server running in the back end, so I am trying to print my req.body in the controller:

exports.test = async (req, res, next) => {
  console.log(req.body);
  res.status(200).json({
    status: "success",
    message: "done",
  });
};

But I am getting req.body as undefined. I am also using CORS.
But if I make Postman requests, it’s working fine. Where am I going wrong?

>Solution :

In your cors set it to *, to allow all.

Change your code to:

const res = await axios({
    method: "POST",
    url: `http://127.0.0.1:8000/api/v1/chatGPT`,
    data: {
      chat: `hello how are you ?`,
    },
  });

Leave a Reply