Best way - HTTP Response Response parsing
Posted: Wed Mar 27, 2019 8:37 am
Hi,
I am sending data to http server which returns a response which generates an event HTTP_EVENT_ON_DATA. I need to parse the json response which I am able to do by using flag.
Like I set a flag whenever I am posting data on some url and check that flag in HTTP_EVENT_ON_DATA and accordingly call the parse fuction written by me using cJson library, which obviously parses all the values.
My concern is that there is lot of delay in getting the complete response which make my whole code very slow.
Could you please guide some better way to acheive parsing in less time???
Also, I checked the response time on postman for each url I am using. None of them is above 300ms.From my code I get response after 3000ms which is way higher to digest.
Below is the snippet of code for your understanding:
This is just one api post to http server , similarly there are 7 api either to post or get, all of which gives the response.
If I am clear with the question, looking forward for the response.
If I am not clear, let me know which part is not clear.
Please consider this as very urgent.
Thanks
Ritu.
I am sending data to http server which returns a response which generates an event HTTP_EVENT_ON_DATA. I need to parse the json response which I am able to do by using flag.
Like I set a flag whenever I am posting data on some url and check that flag in HTTP_EVENT_ON_DATA and accordingly call the parse fuction written by me using cJson library, which obviously parses all the values.
My concern is that there is lot of delay in getting the complete response which make my whole code very slow.
Could you please guide some better way to acheive parsing in less time???
Also, I checked the response time on postman for each url I am using. None of them is above 300ms.From my code I get response after 3000ms which is way higher to digest.
Below is the snippet of code for your understanding:
Code: Select all
esp_err_t _http_event_handler(esp_http_client_event_t *evt)
{
switch(evt->event_id)
{
case HTTP_EVENT_ERROR:
ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
break;
case HTTP_EVENT_ON_CONNECTED:
ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
break;
case HTTP_EVENT_HEADER_SENT:
ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
break;
case HTTP_EVENT_ON_HEADER:
ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
break;
case HTTP_EVENT_ON_DATA:
ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
if (esp_http_client_is_chunked_response(evt->client)) {
printf("%.*s", evt->data_len, (char*)evt->data);
if(g_boolCaptureDeviceInitResponse == true)
{
printf("calling Parse_ServerData\n");
parse_DeviceInit_Response((char*)evt->data);
g_boolCaptureDeviceInitResponse = false;
}
}
else if (!esp_http_client_is_chunked_response(evt->client)) {
//Write out data
printf("%.*s", evt->data_len, (char*)evt->data);
}
break;
case HTTP_EVENT_ON_FINISH:
ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
break;
case HTTP_EVENT_DISCONNECTED:
ESP_LOGD(TAG, "HTTP_EVENT_DISCONNECTED");
break;
}
return ESP_OK;
}
static void device_Init()
{
g_boolCaptureDeviceInitResponse = true;
call_DeviceInit_Api();
if(bSettings_mode == true){
printf("...again generating tokens...\n");
g_boolCaptureGenerateTokenResponse = true;
generate_Tokens();
}
}
static void call_DeviceInit_Api()
{
ESP_LOGI(GTAG,"get server data task started \n");
char new_initURL[100] = {0};
bzero(new_initURL, sizeof(new_initURL));
form_DeviceInitURL(new_initURL);
esp_http_client_config_t config = {
.url = new_initURL,
.event_handler = _http_event_handler,
};
printf("Config URL = %s\n", config.url);
esp_http_client_handle_t client = esp_http_client_init(&config);
esp_http_client_set_header(client, "Content-Type", "application/json");
esp_http_client_set_header(client, "Authorization", access_token);
esp_err_t err = esp_http_client_perform(client);
if(err == ESP_OK) {
ESP_LOGI(HTAG, "Get Server Data GET Status = %d, content_length = %d",
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
g_boolDeviceNotInitiated = false;
} else {
ESP_LOGE(HTAG, "Get Server Data HTTP GET request failed: %s", esp_err_to_name(err));
g_boolDeviceNotInitiated = true;
}
esp_http_client_cleanup(client);
}
static void parse_DeviceInit_Response(char *monitor)
{
printf("Data received in parse_DeviceInit_Response\n");
cJSON *date_time = NULL;
cJSON *device_group_id = NULL;
cJSON *enabled = NULL;
cJSON *settings_mode = NULL;
cJSON *smart_access = NULL;
cJSON *missExpToken = NULL;
cJSON *monitor_json = cJSON_Parse(monitor);
if (monitor_json == NULL)
{
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL)
{
fprintf(stderr, "Error before: %s\n", error_ptr);
}
}
else
{
missExpToken = cJSON_GetObjectItem(monitor_json, "errorMessage");
if(cJSON_IsString(missExpToken) && (missExpToken->valuestring != NULL)){
strcpy(g_byErrMsg_Buff_ReceivedFromServer, missExpToken->valuestring);
g_boolErrorReceivedInTokens = true;
g_boolDeviceInitResponse_ErrorReceived = true;
}
else
{
//printf("Data received in monitor_json\n");
date_time = cJSON_GetObjectItemCaseSensitive(monitor_json, "date_time");
if (cJSON_IsString(date_time) && (date_time->valuestring != NULL))
{
strcpy(g_byTime_DateBuff_ReceivedFromServer, date_time->valuestring);
enabled = cJSON_GetObjectItem(monitor_json, "enabled");
if (cJSON_IsTrue(enabled) == 1){
g_boolEnabledStatus_ReceivedFromServer = enabled->valueint;
}
device_group_id = cJSON_GetObjectItemCaseSensitive(monitor_json, "device_group_id");
if(cJSON_IsString(device_group_id) && (device_group_id->valuestring != NULL))
{
strcpy(g_byDeviceIDBuff_ReceivedFromServer , device_group_id->valuestring);
int len = strlen(g_byDeviceIDBuff_ReceivedFromServer);
if((len == 0) && (g_boolEnabledStatus_ReceivedFromServer != 1)){
gpio_set_level(SERVER_LED_OUTPUT_PIN, 0);
}
else if((len > 1) && (g_boolEnabledStatus_ReceivedFromServer == 1)){
gpio_set_level(SERVER_LED_OUTPUT_PIN, 1);
}
}
settings_mode = cJSON_GetObjectItem(monitor_json, "settings_mode");
if (cJSON_IsTrue(settings_mode) == 1){
printf("Checking settings_mode \"%d\"\n", settings_mode->valueint);
bSettings_mode = settings_mode->valueint;
g_boolSettingsMode_ReceivedFromServer = settings_mode->valueint;
}
smart_access = cJSON_GetObjectItem(monitor_json, "smart_access");
if (cJSON_IsTrue(smart_access) == 1){
printf("Checking smart_access \"%d\"\n", smart_access->valueint);
g_boolSmartAccessStatus_ReceivedFromServer = smart_access->valueint;
}
}
cJSON_Delete(monitor_json);
}
}
}
If I am clear with the question, looking forward for the response.
If I am not clear, let me know which part is not clear.
Please consider this as very urgent.
Thanks
Ritu.