javascriptgrafana-loki

How to do Grafana Loki HTTP API in Javascript


I need to do this kind of Grafana Loki HTTP API in javascript.

Something like this:

$ curl -G -s "http://whatever:3100/loki/api/v1/query_range" --data-urlencode 'query={job="bobjob", branch=~"master"} |= ` ` '

How do I do that in javascript?


Solution

  • @Wyck was right about the POST thing. That's the short answer.

    But to give the long answer for any future person reading this who needs it spelled out a bit more:

    fetch("http://whatever:3100/loki/api/v1/query_range", {
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        body: 'query={job="bobjob", branch=~"master"} |= ` ` '
    })
        .then((response) => response.json())
        .then((obj) => {
            console.log(obj);
        })
        .catch(console.error);
    

    When VIA is connected, it works. When it's not, the CORS issue reemerges.