pihole api - use keepalive for curl queries

Adds the keepalive header to all curl requests

This reduces session establishment time across the multiple

requests necessary to authenticate, obtain response and log out

Signed-off-by: Rob Gill <rrobgill@protonmail.com>
This commit is contained in:
Rob Gill
2025-07-26 18:48:32 +10:00
parent a9680db218
commit 5b4a7b8b74

View File

@@ -55,7 +55,7 @@ TestAPIAvailability() {
API_URL="${API_URL#\"}" API_URL="${API_URL#\"}"
# Test if the API is available at this URL, include delimiter for ease in splitting payload # Test if the API is available at this URL, include delimiter for ease in splitting payload
authResponse=$(curl --connect-timeout 2 -skS -w ">>%{http_code}" "${API_URL}auth") authResponse=$(curl --connect-timeout 2 -skS -H "Connection: keep-alive" -w ">>%{http_code}" "${API_URL}auth")
# authStatus is the response http_code, eg. 200, 401. # authStatus is the response http_code, eg. 200, 401.
# Shell parameter expansion, remove everything up to and including the >> delim # Shell parameter expansion, remove everything up to and including the >> delim
@@ -184,7 +184,7 @@ LoginAPI() {
} }
Authentication() { Authentication() {
sessionResponse="$(curl --connect-timeout 2 -skS -X POST "${API_URL}auth" --user-agent "Pi-hole cli" --data "{\"password\":\"${password}\", \"totp\":${totp:-null}}" )" sessionResponse=$(curl --connect-timeout 2 -skS -H "Connection: keep-alive" -X POST "${API_URL}auth" --user-agent "Pi-hole cli" --data "{\"password\":\"${password}\", \"totp\":${totp:-null}}")
if [ -z "${sessionResponse}" ]; then if [ -z "${sessionResponse}" ]; then
echo "No response from FTL server. Please check connectivity" echo "No response from FTL server. Please check connectivity"
@@ -219,7 +219,7 @@ LogoutAPI() {
# SID is not null (successful Authentication only), delete the session # SID is not null (successful Authentication only), delete the session
if [ "${validSession}" = true ] && [ ! "${SID}" = null ]; then if [ "${validSession}" = true ] && [ ! "${SID}" = null ]; then
# Try to delete the session. Omit the output, but get the http status code # Try to delete the session. Omit the output, but get the http status code
deleteResponse=$(curl -skS -o /dev/null -w "%{http_code}" -X DELETE "${API_URL}auth" -H "Accept: application/json" -H "sid: ${SID}") deleteResponse=$(curl -skS -o /dev/null -w "%{http_code}" -X DELETE "${API_URL}auth" -H "Accept: application/json" -H "sid: ${SID}" -H "Connection: keep-alive")
case "${deleteResponse}" in case "${deleteResponse}" in
"401") echo "Logout attempt without a valid session. Unauthorized!";; "401") echo "Logout attempt without a valid session. Unauthorized!";;
@@ -233,7 +233,7 @@ LogoutAPI() {
GetFTLData() { GetFTLData() {
local data response status local data response status
# get the data from querying the API as well as the http status code # get the data from querying the API as well as the http status code
response=$(curl -skS -w "%{http_code}" -X GET "${API_URL}$1" -H "Accept: application/json" -H "sid: ${SID}" ) response=$(curl -skS -w "%{http_code}" -X GET "${API_URL}$1" -H "Accept: application/json" -H "sid: ${SID}" -H "Connection: keep-alive")
if [ "${2}" = "raw" ]; then if [ "${2}" = "raw" ]; then
# return the raw response # return the raw response
@@ -260,7 +260,7 @@ GetFTLData() {
PostFTLData() { PostFTLData() {
local data response status local data response status
# send the data to the API # send the data to the API
response=$(curl -skS -w "%{http_code}" -X POST "${API_URL}$1" --data-raw "$2" -H "Accept: application/json" -H "sid: ${SID}" ) response=$(curl -skS -w "%{http_code}" -X POST "${API_URL}$1" --data-raw "$2" -H "Accept: application/json" -H "sid: ${SID}" -H "Connection: keep-alive")
# data is everything from response without the last 3 characters # data is everything from response without the last 3 characters
if [ "${3}" = "status" ]; then if [ "${3}" = "status" ]; then
# Keep the status code appended if requested # Keep the status code appended if requested