1

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.

HDIK
  • 33
  • 6
  • I've tried following this https://stackoverflow.com/questions/36840396/fetch-gives-an-empty-response-body - Without any success tho. – HDIK Apr 08 '22 at 15:15

1 Answers1

1

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)

Yami
  • 71
  • 3
  • oh, thank you! I was reading this https://discourse.pi-hole.net/t/pi-hole-api/1863 which doesn't mention the need for auth for this end point, but looking at the api source file, I can now see it does require it, thank you. – HDIK Apr 08 '22 at 16:03