Home > OS >  Cannot add data into elastic using axios from nodejs
Cannot add data into elastic using axios from nodejs

Time:06-28

I am trying to import data into elastic search using axios and nodejs but I am getting 400 bad request error, I have created index in elastic which is sales-enable-data index I am trying to add data using that index but I am getting bad request error

Code

  app.get("/import/data", async (req, res) => {

    const current = {
    project_name: "java",
    delivery_manager: "Yawhe",
    project_manager: "Ruth",
    };
    const data = JSON.stringify(current);

    await axios({
       url: "http://localhost:9200/sales-enable-data-index/_doc",
       method: "POST",
       headers: {
         "Content-Type": "application/json",
       },
       auth: {
         username: "username",
         password: "password",
       },
       body: data,
     })
        .then((response) => {
          return res.status(200).send(response);
       })
        .catch((err) => {
          return res.status(500).send(err);
       });
   });

Error

"code": "ERR_BAD_REQUEST",
"status": 400

CodePudding user response:

Check the Axios request. Request unable to find the endpoint

CodePudding user response:

You're sending String instead of JSON, this is why ElasticSearch says it does not like the data it's receiving.

Try doing the request without the JSON.stringify like this:

app.get("/import/data", async (req, res) => {

  const current = {
    project_name: "java",
    delivery_manager: "Yawhe",
    project_manager: "Ruth",
  };

  await axios({
    url: "http://localhost:9200/sales-enable-data-index/_doc",
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    auth: {
      username: "username",
      password: "password",
    },
    body: current,
  })
      .then((response) => {
        return res.status(200).send(response);
    })
      .catch((err) => {
        return res.status(500).send(err);
    });
});
  • Related