How to save response data from server after HTTPS request
Posted: Mon Dec 04, 2023 11:22 pm
Hi,
I need a help.
In my project I send data to server by HTTP POST request and next i get response from server. I would like to save received data and use it in main function.
I use example of http chunked client which I find on github:
In my project i have function "on_client_data" and "fetch_quote" in other file than main.c . Evrything is ok, but when I want to printf buffer data in main function then ESP is reboting. Here is screen from terminal: 1.png. (in this example I have a wrong request data but it work)
I try to printf response data in main function by:
But anything doesn't work.
Did somebody have a answer where I make a mistake?
I use in my project ESP32 WROOM.
I am waiting for your answer and I will be very grateful for your help.
Adam
I need a help.
In my project I send data to server by HTTP POST request and next i get response from server. I would like to save received data and use it in main function.
I use example of http chunked client which I find on github:
Code: Select all
#include <stdio.h>
#include <string.h>
#include "connect.h"
#include "esp_http_client.h"
#include "esp_log.h"
#include "nvs_flash.h"
static const char *TAG = "REST";
typedef struct chunk_payload_t
{
uint8_t *buffer;
int buffer_index;
} chunk_payload_t;
esp_err_t on_client_data(esp_http_client_event_t *evt)
{
switch (evt->event_id)
{
case HTTP_EVENT_ON_DATA:
{
// ESP_LOGI(TAG, "Length=%d", evt->data_len);
// printf("%.*s\n", evt->data_len, (char *)evt->data);
chunk_payload_t *chunk_payload = evt->user_data;
chunk_payload->buffer = realloc(chunk_payload->buffer, chunk_payload->buffer_index + evt->data_len + 1);
memcpy(&chunk_payload->buffer[chunk_payload->buffer_index],(uint8_t *) evt->data, evt->data_len );
chunk_payload->buffer_index = chunk_payload->buffer_index + evt->data_len;
chunk_payload->buffer[chunk_payload->buffer_index] = 0;
printf("buffer******** %s\n",chunk_payload->buffer);
}
break;
default:
break;
}
return ESP_OK;
}
void fetch_quote()
{
chunk_payload_t chunk_payload = {0};
esp_http_client_config_t esp_http_client_config = {
.url = "http://quotes.rest/qod",
.method = HTTP_METHOD_GET,
.event_handler = on_client_data,
.user_data = &chunk_payload};
esp_http_client_handle_t client = esp_http_client_init(&esp_http_client_config);
esp_http_client_set_header(client, "Contnet-Type", "application/json");
esp_err_t err = esp_http_client_perform(client);
if (err == ESP_OK)
{
ESP_LOGI(TAG, "HTTP GET status = %d, result = %s\n",
esp_http_client_get_status_code(client), chunk_payload.buffer);
}
else
{
ESP_LOGE(TAG, "HTTP GET request failed: %s", esp_err_to_name(err));
}
if(chunk_payload.buffer != NULL){
free(chunk_payload.buffer);
}
esp_http_client_cleanup(client);
wifi_disconnect();
}
void app_main(void)
{
ESP_ERROR_CHECK(nvs_flash_init());
wifi_init();
ESP_ERROR_CHECK(wifi_connect_sta("POCO", "password", 10000));
fetch_quote();
}
I try to printf response data in main function by:
Code: Select all
printf("buffer %s\n","name of structure"->buffer);
ESP_LOGI("ESP32", "buffer = %s", "name of structure".buffer);
Did somebody have a answer where I make a mistake?
I use in my project ESP32 WROOM.
I am waiting for your answer and I will be very grateful for your help.
Adam