无法使用nodejs的Axios将数据添加到弹性中

发布于 2025-02-11 07:41:18 字数 950 浏览 3 评论 0原文

我正在尝试使用Axios和nodejs将数据导入弹性搜索中,但是我遇到了400个不良请求错误,我已经在弹性中创建了索引,这是sales-enable-data索引,我正在尝试使用该索引添加数据,但是我变得越来越糟请求错误

代码

  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);
       });
   });

错误

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千仐 2025-02-18 07:41:18

检查Axios请求。请求无法找到端点

Check the Axios request. Request unable to find the endpoint

橙味迷妹 2025-02-18 07:41:18

您是发送字符串而不是JSON,这就是为什么Elasticsearch表示它不喜欢收到的数据的原因。

尝试在没有json.stringify的情况下执行请求:

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);
    });
});

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);
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文