Page 1 of 1

Upload file from ESP32 SPIFFS to URL via esp_http_client

Posted: Fri Jan 06, 2023 8:23 am
by twinkle.goyal
Hi,
I am using ESP32S3 along with ESP-IDF in C. I need to upload a file stored in the SPIFFS to an URL using an HTTP post request.
I discovered that I must use esp_http_client and application/form-data for this since the file size is < 10KB. I am new to esp and am facing trouble with this.

The Linux command which works is:
curl --location --request POST 'https://url' --form 'filename=@"/path/to/file.log"'

I need to know how can I achieve the same through esp_http_client APIs. I have seen some examples online for posting a JSON but have not found any for a file.

I have written a partial code like this:
{
//Initialize spiffs
esp_http_client_config_t config_post =
{
.url = "https://url",
.method = HTTP_METHOD_POST,
.cert_pem = NULL
};
esp_http_client_handle_t client = esp_http_client_init(&config_post);
esp_http_client_set_post_field(<What do I write here? File location: /spiffs/file.log>);
esp_http_client_set_header(client, "Content-Type", "application/form-data");
esp_http_client_perform(client);
esp_http_client_cleanup(client);
}

I would really appreciate any help.
Thank you.