Home > database >  fetch returns empty body, json works, but plain text doesn't
fetch returns empty body, json works, but plain text doesn't

Time:04-09

I am trying to create a function which return the latest blocked domain from my pihole server.

I first created a JSON call, since the first call I needed was in JSON format, this is all working and I get the data needed.

However, the second function I need is to fetch plain text data and that one doesn't work, it simply returns an empty body [].

This is the function

socket.on("pihole_last", function() {
    setInterval(function() {
        let settings = {
            method: "Get",
            headers: {
                "Accept": "text/html"
            }
        };

        fetch('http://domain/admin/api.php?recentBlocked', settings)
            .then(res => res.text())
            .then((data) => {
                console.log(data);
            }).catch(error => {
                return error;
            });;
    }, 1000)
});

The JSON function which works looks pretty much the same, the only real different is the header accept and the res.text() which should fetch the data in plain text? The data returned from the URL is a plain text domain, no tags, no nothing.

CodePudding user response:

According to this issue from the pi-hole GIT, you should provide some form of authentication. The question which you linked in your comment is 5 years old, at that time this was an unintended behaviour.

If I understand correctly the API description one way to authorize should be working with this url: http://domain/admin/api.php?recentBlocked?auth=YOUR_TOKEN

The YOUR_TOKEN should be in:

Authorization & Token required (see WEBPASSWORD in /etc/pihole/setupVars.conf)

  • Related