programowanie:esp32:http

HTTP na ESP32


#include <HTTPClient.h>


if(WiFi.status()== WL_CONNECTED)
{
  String url = "https://sases.pl";
 
  HTTPClient httpClient; // Utworzenie instancji
  httpClient.begin(url.c_str());
  int httpResponseCode = httpClient.GET();
 
  if (httpResponseCode > 0) 
  {
    if (httpResponseCode == 200)
    {
      String payload = httpClient.getString();  // String z zawartością URL
    }
  }
  httpClient.end(); // Zniszczenie instancji i zwolnienie zasobów
}



POST request

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();
}
  • programowanie/esp32/http.txt
  • ostatnio zmienione: 2024/10/09 20:12
  • przez sases