programowanie:esp32:http

Różnice

Różnice między wybraną wersją a wersją aktualną.

Odnośnik do tego porównania

Poprzednia rewizja po obu stronach Poprzednia wersja
Nowa wersja
Poprzednia wersja
programowanie:esp32:http [2024/10/07 15:54] sasesprogramowanie:esp32:http [2024/10/09 20:12] (aktualna) sases
Linia 2: Linia 2:
 \\ \\
  
-=== Import ===+===== Import =====
 <code c> <code c>
 #include <HTTPClient.h> #include <HTTPClient.h>
Linia 8: Linia 8:
 \\ \\
  
-=== HTTP GET request ===+===== GET request =====
 <code c> <code c>
-String url = "https://sases.pl"; 
- 
 if(WiFi.status()== WL_CONNECTED) if(WiFi.status()== WL_CONNECTED)
 { {
 +  String url = "https://sases.pl";
 +
   HTTPClient httpClient; // Utworzenie instancji   HTTPClient httpClient; // Utworzenie instancji
   httpClient.begin(url.c_str());   httpClient.begin(url.c_str());
Linia 28: Linia 28:
 } }
 </code> </code>
 +\\
 +\\
 +
 +===== POST request =====
 +<code c>
 +if(WiFi.status() == WL_CONNECTED)
 +{
 +  String url = api_url + "/blinds/" + String(Blinds_Id[id]) + "/";
 +  String payload = "{\"position\":0, \"runtime_up\": " + String(Blinds_Runtime_Up[id]) + ", \"runtime_down\": " + String(Blinds_Runtime_Down[id]) + "}";
 +
 +  HTTPClient httpClient;
 +  httpClient.begin(url);
 +  httpClient.addHeader("Content-Type", "application/json");
 +  httpClient.addHeader("Authorization", "Bearer " + accessToken);
 +  int httpResponseCode = httpClient.POST(payload);
 +
 +  if (httpResponseCode > 0) 
 +  {
 +    if (httpResponseCode == 200)
 +    {
 +      String response = httpClient.getString();
 +      // Serial.println(response);
 +    }
 +    else
 +    {
 +      Serial.print("API Error code: ");
 +      Serial.println(httpResponseCode);
 +    }
 +  }
 +  else
 +  {
 +    Serial.print("API Error on sending POST: ");
 +    Serial.println(httpResponseCode);
 +  }
 +  httpClient.end();
 +}
 +</code>
 +
  • programowanie/esp32/http.1728309257.txt.gz
  • ostatnio zmienione: 2024/10/07 15:54
  • przez sases