Task-Driven, powersaving trial written, must test.
This commit is contained in:
@@ -63,6 +63,59 @@ bool httpGET(std::string endpoint, std::string token, cJSON* &JSONresponse) {
|
||||
return success;
|
||||
}
|
||||
|
||||
bool httpPOST(std::string endpoint, std::string token, cJSON* postData, cJSON* &JSONresponse) {
|
||||
std::string url = urlBase + endpoint;
|
||||
std::string responseBuffer = "";
|
||||
|
||||
// Convert JSON object to string
|
||||
char* postString = cJSON_PrintUnformatted(postData);
|
||||
if (postString == NULL) {
|
||||
printf("Failed to serialize JSON for POST\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
esp_http_client_config_t config = {};
|
||||
config.url = url.c_str();
|
||||
config.method = HTTP_METHOD_POST;
|
||||
config.event_handler = _http_event_handler;
|
||||
config.user_data = &responseBuffer;
|
||||
if (secureSrv) {
|
||||
config.transport_type = HTTP_TRANSPORT_OVER_SSL;
|
||||
config.crt_bundle_attach = esp_crt_bundle_attach;
|
||||
}
|
||||
|
||||
esp_http_client_handle_t client = esp_http_client_init(&config);
|
||||
|
||||
// Set headers
|
||||
std::string authHeader = "Bearer " + token;
|
||||
esp_http_client_set_header(client, "Authorization", authHeader.c_str());
|
||||
esp_http_client_set_header(client, "Content-Type", "application/json");
|
||||
|
||||
// Set POST data
|
||||
esp_http_client_set_post_field(client, postString, strlen(postString));
|
||||
|
||||
// Perform request
|
||||
esp_err_t err = esp_http_client_perform(client);
|
||||
bool success = false;
|
||||
|
||||
if (err == ESP_OK) {
|
||||
int status_code = esp_http_client_get_status_code(client);
|
||||
printf("Status = %d, Content Length = %d\n", status_code, esp_http_client_get_content_length(client));
|
||||
|
||||
if (status_code == 200 || status_code == 201) {
|
||||
printf("Response: %s\n", responseBuffer.c_str());
|
||||
JSONresponse = cJSON_Parse(responseBuffer.c_str());
|
||||
if (JSONresponse) success = true;
|
||||
}
|
||||
} else {
|
||||
printf("HTTP POST failed: %s\n", esp_err_to_name(err));
|
||||
}
|
||||
|
||||
free(postString);
|
||||
esp_http_client_cleanup(client);
|
||||
return success;
|
||||
}
|
||||
|
||||
void deleteWiFiAndTokenDetails() {
|
||||
nvs_handle_t wifiHandle;
|
||||
if (nvs_open(nvsWiFi, NVS_READWRITE, &wifiHandle) == ESP_OK) {
|
||||
|
||||
Reference in New Issue
Block a user