Trying to get InfluxDB version with a simple html/javascript form

I am trying to get InfluxDB version with a simple html/javascript form but i am getting into troubles.

My environment: RaspberryPi CM4, RaspberryPi OS 64bit, InfluxDB Docker(influxdb:latest)

1. /api/v2/ping returns 404

{
	"code": "not found",
	"message": "path not found"
}

2. I can not access X-Influxdb-Version header, probably do to Access-Control-Expose-Headers is missing

HTTP/1.1 404 Not Found
Access-Control-Allow-Origin: null
Content-Type: application/json; charset=utf-8
X-Influxdb-Build: OSS
X-Influxdb-Version: v2.7.4
X-Platform-Error-Code: not found
Date: Mon, 04 Dec 2023 12:37:05 GMT
Content-Length: 54

InfluxDB 1.8.10 /ping request is fine and i can access X-Influxdb-Version header

HTTP/1.1 204 No Content
Access-Control-Allow-Headers: Accept, Accept-Encoding, Authorization, Content-Length, Content-Type, X-CSRF-Token, X-HTTP-Method-Override
Access-Control-Allow-Methods: DELETE, GET, OPTIONS, POST, PUT
Access-Control-Allow-Origin: null
Access-Control-Expose-Headers: Date, X-InfluxDB-Version, X-InfluxDB-Build
Content-Type: application/json
Request-Id: f9d2d2a6-9359-11ee-80e6-0242ac11000a
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.8.10
X-Request-Id: f9d2d2a6-9359-11ee-80e6-0242ac11000a
Date: Tue, 05 Dec 2023 10:35:14 GMT

Any help on this?

Test it yourself:

<!doctype html>
<html>

<head>
    <title>Get InfluxDB 2 version</title>
    <meta charset="UTF-8">
</head>

<body>
    <form id="form">
        <table>
            <tr>
                <td>Url:</td>
                <td><input id="influx_url" type="text" size="50" value="http://yourhost:8086/api/v2/ping"></td>
            </tr>
            <tr>
                <td>Tocken:</td>
                <td><input id="influx_tocken" type="text" size="50" value="yourtocken"></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><button>Get Version</button></td>
            </tr>
        </table>
    </form>
    <br>
    Version: <span id="influx_version"></span>
    <br>
    Headers: <span id="influx_header"></span>
    <script>
        function _id(el) {
            return document.getElementById(el);
        }

        function parseHttpHeaders(httpHeaders) {
            return httpHeaders.toLowerCase()
                .split("\n")
                .map(x => x.split(/: */, 2))
                .filter(x => x[0])
                .reduce((ac, x) => { ac[x[0]] = x[1]; return ac; }, {});
        }
        function getVersion() {
            const request = new XMLHttpRequest();
            request.open("POST", _id('influx_url').value);
            request.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
            request.setRequestHeader("Authorization", "Token " + _id('influx_tocken').value);
            request.setRequestHeader("Accept", "application/json");
            request.onreadystatechange = () => {
                var headers = parseHttpHeaders(request.getAllResponseHeaders());
                _id('influx_version').innerHTML = headers['x-influxdb-version'];
                _id('influx_header').innerHTML = request.getAllResponseHeaders().toLowerCase();
            };
            request.send();
        }
        const form = _id("form");
        form.addEventListener("submit", function (event) {
            event.preventDefault();
            getVersion();
        });
    </script>
</body>

</html>

@SeventyNine With InfluxDB v2, you should use the /health endpoint. It returns the version as part of a JSON object: InfluxDB v2 API /health endpoint.

@scott I am confused because in /docs it is clearly /api/v2/health
. I like the fact that there is a documentation built in, but useless if it’s not correct!

Indeed /health returns a json string if I call it directly in a browser.

  "name": "influxdb",
  "message": "ready for queries and writes",
  "status": "pass",
  "checks": [
    
  ],
  "version": "v2.7.4",
  "commit": "19e5c0e1b7"
}

In my example with javascript I am getting a CORS error: Reason: CORS header ‘Access-Control-Allow-Origin’ missing. Any help on this?

Hmm, interesting. Yeah, I’m not sure how well-maintained the built-in documentation is, but the publicly accessible API docs are correct.

CORS issues are always “fun” to deal with. Looks like others have run into it too and it’s discussed in JavaScript influxdb-client react web app CORS issue .

My goal was to verify InfluxDB configuration on a ESP8266 sensor.

First I tried using only javascript but failed do to intelligence of a modern browser. Finally I use ESP8266 to do a HTTP request from InfluxDB server and use javascript only to get the result from ESP8266. This way it is’t restricted to read headers nor content.

Files for testing are here: GitHub - blenherr/ESP8266-InfluxDB-Verifier: This is a way to verify your InfluxDB configuration on a ESP8266 while AsyncWebServer is running.