Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send query parameters with Axios?

My question is similar to this How to post query parameters with Axios? Instead of posting, I want a get data request and I wanna pass the query param name to the request. It works in postman but not working in react.

const handleSubmit = async () => {
try {
  const res = await axios.get(
    "http://localhost:5000/api/products",
    {},
    {
      params: {
        name,
      },
    }
  );
  console.log(res.data);
} catch (err) {}
};





 exports.retrieveProducts = (req, res) => {
   Product.find(
    { name: { $regex: req.query.name, $options: "i" } },
    (err, products) => {
      if (err) res.status(500).json(err);
      res.json(products);
    }
  );
};
like image 901
Undisclosed Avatar asked Jan 31 '26 04:01

Undisclosed


1 Answers

You are using an empty object as config.

It should be

const handleSubmit = async () => {
try {
  const res = await axios.get(
    "http://localhost:5000/api/products",
    {
      params: {
        name: 'whatever',
      },
    }
  );
  console.log(res.data);
} catch (err) {}
};
like image 52
Apostolos Avatar answered Feb 01 '26 19:02

Apostolos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!