From 741e0ee5b3dc0648406e0a987180e61021c6eb78 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Tue, 16 Aug 2022 20:44:33 +0200 Subject: [PATCH] 2fb: use POST for non-idempotent API endpoints --- ITS_Open_direct_2fb/ITS_Open_direct_2fb.ino | 41 ++++----------------- 1 file changed, 8 insertions(+), 33 deletions(-) diff --git a/ITS_Open_direct_2fb/ITS_Open_direct_2fb.ino b/ITS_Open_direct_2fb/ITS_Open_direct_2fb.ino index d6a4874..12f27d4 100644 --- a/ITS_Open_direct_2fb/ITS_Open_direct_2fb.ino +++ b/ITS_Open_direct_2fb/ITS_Open_direct_2fb.ino @@ -288,17 +288,17 @@ void readServerStatus(int mode) { client.stop(); } -void TriggerServerReq(String s, int mode) { +void TriggerServerReq(String req, int mode) { // attempt to connect, and wait a millisecond: if (debug) { Serial.println("connecting to server... Status req"); } if (client.connect(SERVER_DOMAIN, 80)) { if (debug) { - Serial.println("making HTTP request..."); + Serial.println("making HTTP request: " + req); } - // make HTTP GET request to server: - client.println("GET " + s + " HTTP/1.1"); + // make HTTP request to server: + client.println(req + " HTTP/1.1"); client.println("HOST: " SERVER_DOMAIN); client.println("Connection: close"); client.println(); @@ -312,7 +312,7 @@ void TriggerServerReq(String s, int mode) { } void RequestState() { - TriggerServerReq("/api/status-s.php", 0); + TriggerServerReq("GET /api/status-s.php", 0); } // Status LED pins @@ -348,46 +348,21 @@ void setup() { RequestState(); } -void TriggerServerUpdate(boolean stat) { - // attempt to connect, and wait a millisecond: - if (debug) { - Serial.println("connecting to server... Update Req"); - } - if (client.connect(SERVER_DOMAIN, 80)) { - if (debug) { - Serial.println("making HTTP request..."); - } - // make HTTP GET request to server: - String s = (stat ? "true" : "false"); - client.println("GET /api/update.php?open=" + s + "&apikey=" UPDATE_API_KEY + - " HTTP/1.1"); - client.println("HOST: " SERVER_DOMAIN); - client.println("Connection: close"); - client.println(); - readServerStatus(0); - } else { - if (debug) { - Serial.println("Not connected..."); - } - } - // note the time of this connect attempt: -} - void readButtons() { if ((digitalRead(topen) == LOW) && (hsopen != 1)) { // startEthernet(); setRoom(2); - TriggerServerUpdate(true); + TriggerServerReq("POST /api/update.php?open=true&apikey=" UPDATE_API_KEY, 0); } if ((digitalRead(tclose) == LOW) && (hsopen != 0)) { // startEthernet(); setRoom(2); - TriggerServerUpdate(false); + TriggerServerReq("POST /api/update.php?open=false&apikey=" UPDATE_API_KEY, 0); } } void RequestPing() { - TriggerServerReq("/api/ping-get.php?apikey=" PING_GET_API_KEY, 1); + TriggerServerReq("POST /api/ping-get.php?apikey=" PING_GET_API_KEY, 1); } void launchUpdate() {