API Login und usage via CURL (API v.6)

Hi all,

is there a possibility to use cURL in combination with the new v6 API?

I was successfully able to create the following command to deactivate blocking for 30 seconds. But this only works if I disable the password authentication.

curl -X POST "http://192.168.253.10:8800/api/dns/blocking" -H "content-type: application/json" -d "{""blocking"": false, ""timer"":30}"

Remark: I used this cURL command under Windows, therefore the usage of "". My main goal is to create a small .bat file which I can place on my desktop.

But with a disabled password I was not able to logon to the webinterface because the webinterfaces forces me to enter a password even having none set for the installation. If I now activate the password again the above command is not working because I first would need to authenticate.

My question now is how I can use authentication and the above command combined via cURL?

Thanks a lot for any hint.

Cheers,
Tommy

You need to pass your authentication.

that I need to pass the authentication was clear but I did not know how to combine both commands.

I found the following solution:

  • check URL and port and adjust
  • replace the password with your own one "YOUR-PASSWORD"
  • download /jq, rename it to "jq.exe" and place it into the same directly where the ".bat" file is stored --> Download jq
@echo off
setlocal enabledelayedexpansion

:: ==== Define paths ====
:: Get the directory of the batch file (to find jq.exe)
set "SCRIPT_DIR=%~dp0"
set "JQ=%SCRIPT_DIR%jq.exe"

:: ==== Configuration ====
set "LOGIN_URL=http://pi.hole:80/api/auth"
set "SECOND_URL=http://pi.hole:80/api/dns/blocking"
set "LOGIN_JSON={\"password\":\"YOUR-PASSWORD\"}"

:: ==== First POST request and extract .session.sid ====
for /f "delims=" %%A in (
    'curl -s -X POST -H "Content-Type: application/json" -d "%LOGIN_JSON%" "%LOGIN_URL%" ^| "%JQ%" -r ".session.sid"'
) do (
    set "SID=%%A"
)

:: ==== Display the SID ====
:: echo Retrieved SID: !SID!

:: ==== Prepare JSON - disable DNS blocking for 30 seconds ====
set "SECOND_JSON={\"sid\":\"!SID!\",\"blocking\": false, ""timer"":30}"

:: ==== Second POST request using SID ====
curl -s -X POST -H "Content-Type: application/json" -d "!SECOND_JSON!" "%SECOND_URL%"

:: ==== Done ====
endlocal

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.