I wanted to post a reply for anyone else doiung this as I had a lot of trouble with sending chunked encoding, so hopefully it will help someone else out:)
The issue that was tripping me up was that my transfers all seemed to fail, and it turns out I was sending the length as decimal numbers - they need to be hexadecimal.
Also of note, if doing mixed transfers with the same handle, remember to delete any used headers before exiting the function, the transfer-encoding header seems to need to be deleted manually before using the handle for something else.
Here is some example code that I'm using - to send the end sequence, use transmit_chunk(""):
Code: Select all
int transmit_chunk(char *string) {
int written_length = 0;
int total_length = 0;
char length_string[11] = "";
if (strlen(string) > 0) {
bzero(length_string, sizeof(length_string));
snprintf(length_string, sizeof(length_string), "%X", strlen(string));
written_length = esp_http_client_write(http_client, length_string, strlen(length_string));
if (written_length > 0) {
total_length += written_length;
} else {
return -1;
}
written_length = esp_http_client_write(http_client, "\r\n", 2);
if (written_length > 0) {
total_length += written_length;
} else {
return -1;
}
ESP_LOGD(UART_TAG, "Sent: %s", length_string);
written_length = esp_http_client_write(http_client, string, strlen(string));
if (written_length > 0) {
total_length += written_length;
} else {
return -1;
}
written_length = esp_http_client_write(http_client, "\r\n", 2);
if (written_length > 0) {
total_length += written_length;
} else {
return -1;
}
ESP_LOGD(UART_TAG, "Sent: %s", string);
} else {
written_length = esp_http_client_write(http_client, "0", 1); // end
if (written_length > 0) {
total_length += written_length;
} else {
return -1;
}
esp_http_client_write(http_client, "\r\n", 2);
if (written_length > 0) {
total_length += written_length;
} else {
return -1;
}
esp_http_client_write(http_client, "\r\n", 2);
if (written_length > 0) {
total_length += written_length;
} else {
return -1;
}
ESP_LOGD(UART_TAG, "Sent http end chunk");
}
return total_length;
}