Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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 ?`,
    },
  });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading