I'm trying to send a .wav file to an API with an ESP32 using a HTTP Post request with a multipart/Form-data content type. I can already send the API Key as it is a plain key and get a return from the server, but I fail on how to add the .wav file into the post request. I am currently reading the wav file from an sd card, it's 5 secs long 16 bit,11025Hz. and about 400 kb. If anyone have experience with this, your help would be greatly appreciated!

This is the code snippet where I send the key and where the data should come; I tried putting the code into a buffer as and concatting the data but I'm not sure if this is the right way to do it char buf[1024];

 if (https.begin(client, host, port, path)) {

   //https.addHeader("Content-Type", "multipart/Form-data");
   //int httpsCode = https.POST("apikey = XXX");
     https.addHeader("content-type", "multipart/form-data; boundary=----123");
    String HTTPSPost ="------123\r\nContent-Disposition: form-data; 


name=\"apikey\"\r\n\r\XXX\r\n------123--\r\nContent-Disposition: form-data; name=\"wav\"; filename=\"test444.wav\"\r\nContent-Type: audio/x-wav\r\n\r\n\r\n";
    //wav recording Comes here
    String WavData ;
   // for (int i = 0; i < 1024; i++) WavData.concat(buf[i]);; // Tried to put Wav file in a buffer 
  // Serial.println(WavData);

    String End = "------123--";
    String full = HTTPSPost + WavData + End;
    int httpsCode = https.POST(full );
        if (httpsCode > 0) {
              if (httpsCode == HTTP_CODE_OK) {
                res =  https.getString();
                JsonObject& obj = jb.parseObject(res);

                String token = obj["error"];
                if (obj.success()) {

                } else {
                    res = "failed to parse json";
        } else {
            res = "failed to Post";
   } else {
    res = "failed to connect to server";
return res;

better code because httpclient can't do sd cards, but wificlient can;

File file = SD.open("/xxx.wav");

if (!client.connect("XXX", 443)) {
  Serial.println("Connected FAILED!");

Serial.println("Connected ok!");

client.println("POST  HTTP/1.1");
client.println("Host: ");
client.println("Cache-Control: no-cache");
client.println("Content-Type: multipart/form-data; boundary=----123");
client.println("Content-Disposition: form-data; name=\"grant_type\"");
client.println("Content-Disposition: form-data; name=\"apikey\"");
client.println("Content-Disposition: form-data; name=\"wav\"; filename=\"xxx.wav\"");
client.println("Content-Type: audio/wav");
client.print("Content-Length: ");
//while (file.available()){
//      client.write(file.read());
if (client.available()) 
      String serverRes = client.readStringUntil('}');
  • what is HTTPSPost+buf[1024]+End? you can't put binary data into a String. a 0 would be end of the data – Juraj Apr 3 '19 at 7:41
  • You're right, it's a leftover from me trying to put the buffer into the string... the Full string should be placed into it, edited that part. – Tom Demuynck Apr 3 '19 at 7:50
  • Is there a way to convert it, or how to make it 'correct'? I guess that is my question – Tom Demuynck Apr 3 '19 at 13:10
  • The esp32 only has 512kb of RAM. Putting a 400kb wave file, plus some HTTP headers, in memory will probably not fit. – Gerben Apr 3 '19 at 13:54
  • you can't do it with HTTPClient. use WiFiClient. if the file is in SPIFFS you can use client.write(file) for the binary part of the POST request – Juraj Apr 3 '19 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.