如何使用简单的 HTML 表单将 post 指标发送到 InfluxDB?

How to post metrics to InfluxDB with a simple HTML form?

我正在尝试通过来自 HTML 表单的 POST 请求将 01 发送到我的 InfluxDB 实例中的数据库。我已经通过 curl 成功地完成了很多次,但我无法使用简单的 HTML 表单。考虑这个 HTML 代码:

<!doctype html>
<!-- this file is called like http://my.influx.server/my_page_name.html -->
<html>
  <head>
    <title>my simple html/influx sender</title>
    <meta charset="UTF-8"/>
  </head>
  <body>
    <form action="http://my.influx.server:8086/write?db=db_name" method="post" enctype="text/plain">
      <input name="data" type="hidden" value="my_measurement,tag_name=stuff value=1"/>
      <input type="submit" value="insert 1"/>
    </form>

    <form action="http://my.influx.server:8086/write?db=db_name" method="post" enctype="text/plain">
      <input name="data" type="hidden" value="my_measurement,tag_name=stuff value=0"/>
      <input type="submit" value="insert 0"/>
    </form>
  </body>
</html>

发送 1curl 命令类似于:

curl -i -XPOST 'http://my.influx.server:8086/write?db=mydb' --data-binary 'my_measurement,tag_name=stuff value=1'

所以我尝试制作一个只有 2 个按钮的简单 HTML 表单。上面的代码是我至少可以尝试处理 "line interface" 语法的最接近的代码,但是我收到错误消息或只是没有响应,而且我的 InfluxDB 中什么也没有。上面代码的错误消息是:

unable to parse 'data=my_measurement,tag_name=stuff value=1\r': invalid number

如果你仔细查看字符串的末尾,你会看到一个 \r 显然被添加了,我怀疑这会破坏数字解析(我前段时间有类似的东西),但是在至少这似乎试图评估这条线。但是,我还没有找到删除或避免 \r 的方法。 有人知道如何实现吗?

此外,请考虑以下附加信息:


编辑 1:我试图用 curl:

重现错误
curl -i -XPOST 'http://my.influx.server:8086/write?db=home' --data-binary 'my_measurement,tag_name=stuff value=1\r'

这导致错误消息:

unable to parse 'my_measurement,tag_name=stuff value=1\r': invalid number

与 headers:

HTTP/1.1 400 Bad Request
Content-Type: application/json
Request-Id: ...
X-Influxdb-Build: OSS
X-Influxdb-Error: unable to parse 'my_measurement,tag_name=stuff value=1\r': invalid number
X-Influxdb-Version: 1.7.9
X-Request-Id: ...
Date: ...
Content-Length: 78

我得出结论:


编辑 2:我发现了如何通过对 curl 的调用显示请求 headers。命令是:

curl -v -XPOST 'http://my.influx.server:8086/write?db=db_name' --data-binary 'my_measurement,tag_name=stuff value=1'

命令输出的相关部分是:

> POST /write?db=db_name HTTP/1.1
> Host: my.influx.server:8086
> User-Agent: curl/7.58.0
> Accept: */*
> Content-Length: 37
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 37 out of 37 bytes
< HTTP/1.1 204 No Content
< Content-Type: application/json
< Request-Id: ...
< X-Influxdb-Build: OSS
< X-Influxdb-Version: 1.7.9
< X-Request-Id: ...
< Date: Sat, 25 Jan 2020 10:54:11 GMT

我得出结论:

最后,我找到了 JavaScript 有效的解决方案。 This Mozilla doc page 是没有键的 POST 表单的键。我的 HTML 页面现在看起来像这样:

<!doctype html>
<!-- this file is called like http://my.influx.server/my_page_name.html -->
<html>
  <head>
    <title>my simple html/influx sender</title>
    <meta charset="UTF-8"/>
  </head>
  <body>
    <form id="form1">
      <button>insert 1</button>
    </form>

    <form id="form0">
      <button>insert 0</button>
    </form>

    <script>
        function sendData(value)
        {
            const str = "my_measurement,tag_name=stuff value=" + value;
            const xhr = new XMLHttpRequest();

            xhr.addEventListener("load", function(event) {
                alert("Success");
            });

            xhr.addEventListener("error", function(event) {
                alert("Error");
            });

            xhr.open("POST", "http://my.influx.server:8086/write?db=db_name");
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

            xhr.send(str);
        }

        const form0 = document.getElementById("form0");
        const form1 = document.getElementById("form1");

        form0.addEventListener("submit", function(event) {
            event.preventDefault();
            sendData(0);
        });
        form1.addEventListener("submit", function(event) {
            event.preventDefault();
            sendData(1);
        });
    </script>
  </body>
</html>

注意精简的表单定义:不再有 actionmethodenctype,因为它们是通过 JavaScript 设置的。此外,没有常规的 submit 元素,而是一个常规按钮,但我不知道是否需要这样做。我稍后会调查。

主要部分在表单下方的脚本标签中。函数 sendDataPOST 准备好的字符串准备一个 XMLHttpRequest 对象并调用它的 send 方法。该函数用在各个表单的submit事件中。此外,此函数还为成功和失败的请求注册事件处理程序。

sendData 函数下面的行标识表单并在其 submit 事件上注册事件侦听器。每个侦听器都阻止其表单以常规方式提交,而是调用适当的 sendData 调用,这将成功地将值插入 InfluxDB。

但请注意,仍然不能保证检测到每个错误。我试图将一个字符串插入一个整数字段,但失败了,但我仍然收到 "Success"-alert。我稍后会调查。

所以总的来说,我认为这个问题对于我的目的来说已经足够解决了,我希望这能帮助任何遇到这个问题的人。

这非常有用 post,我 运行 解决了 Sigfox 后端和回调的这个问题。

如果您在 URL 末尾放置一个 & 并使用内容类型 text/plain,那么 \r\n 问题就解决了。