send body with "GET" method in axios

Viewed 228

Is there anyway to send body with GET method in axios? because in postman it is possible. My backend code as below:

I'm using express.js + sequelize

const c_p_get_all = async (req, res) => {
const { category } = req.body;
const sql = `select p.id, p.p_image, p.p_name, p.p_desc, p.p_prize, p.p_size, c.c_name, cl.cl_name
  from products as p
  inner join collections as cl on cl.id = p.p_collection_id 
  inner join categories as c on c.id = cl.cl_category_id
  where c.c_name = ?
  order by p."createdAt" desc;`;
 
try {
  const getData = await Product.sequelize.query(sql, {
    replacements: [category],
  });
   if (getData[0] != "") {
     res.status(200).send({
      s: 1,
      message: "success retrive all products",
      data: getData[0],
     });
   } else {
     res.status(404).send({
      s: 0,
      message: "data not found",
  });
  }
 } catch (err) {
   res.status(500).send({
     message: err,
  });
 }
};

My Frontend with react.js + axios

const test = "woman";
axios({
  headers: {
    "content-type": "application/json",
  },
  method: "GET",
  url: "http://localhost:3001/api/v1/product",
  data: { category: test },
})
  .then((value) => console.log(value))
  .catch((error) => console.log(error.response));

It always goes to status 404, but in postman its working, I've tried to search this problem, but no clue. So is there anyway to do it in axios, or should I change my backend to POST method or change req.body to req.query?

1 Answers

I changed to query parameters and it worked

Related