HTTP client response data corrupted
Posted: Fri Aug 23, 2019 7:57 am
I faced and issue with HTTP client when performing POST request.
The code is as followed:
However, after the request is finished I received response data currupted.
After studying the Esp_HTTP_client code I found that when the first data chunk is received (containing headers and body data begin) and parsed client->response->buffer->raw_data pointer points to the place where the body data begins. But the next data chunk is received before calling esp_http_client_read(). As a result new data is placed to the response buffer beginning , hence, pointer client->response->buffer->raw_data is assign to the buffer beginning causing a case that the first data chunk lays at larger address, while the second one starts at lower address (two chunks become swapped).
Could anyone tell me did I have some mistake while using the client or is there some error in it?
Update: I tried the code with both IDF versions 3.2 and 3.2.2, the result was the same.
The code is as followed:
Code: Select all
esp_http_client_config_t config = {
.method=HTTP_METHOD_POST,
.url=Request->Query,
.timeout_ms=CLOUD_LOOKUP_TIMEOUTms,
.disable_auto_redirect=1, //no redirection
.event_handler = HTTP_EventHandler,
};
client = esp_http_client_init(&config);
if(!client)
{
break;
}
if(esp_http_client_set_header(client, "Content-Type", HTTP_CONT_TYPE_BIN) ||
esp_http_client_set_header(client, "Cache-Control", "no-cache") ||
esp_http_client_set_header(client, "User-Agent", HTTP_USERAGENT) ||
esp_http_client_set_header(client, "Accept", "*/*"))
{
break;
}
if(Err=esp_http_client_set_post_field(client, Request->BodyData, Request->BodyDataLen))
{
break;
}
Err = esp_http_client_perform(client);
if (!Err)
{
Request->ReponseCode=esp_http_client_get_status_code(client);
Request->ResponseDataLen=esp_http_client_get_content_length(client);
}
else
{
break;
}
if(Request->ResponseDataLen>HTTP_MAX_RESPONSE_LEN)
{
break;
}
Request->ResponseData=zalloc(Request->ResponseDataLen);
if(!Request->ResponseData)
{
break;
}
u16 RecDataLen=0;
if((RecDataLen=esp_http_client_read(client, Request->ResponseData, Request->ResponseDataLen/*max len*/))==-1)
{
break;
}
After studying the Esp_HTTP_client code I found that when the first data chunk is received (containing headers and body data begin) and parsed client->response->buffer->raw_data pointer points to the place where the body data begins. But the next data chunk is received before calling esp_http_client_read(). As a result new data is placed to the response buffer beginning , hence, pointer client->response->buffer->raw_data is assign to the buffer beginning causing a case that the first data chunk lays at larger address, while the second one starts at lower address (two chunks become swapped).
Could anyone tell me did I have some mistake while using the client or is there some error in it?
Update: I tried the code with both IDF versions 3.2 and 3.2.2, the result was the same.