2

I am trying to post an audio file and a text to a server from ESP32. I can easily do that kind of post request using Postman. Post request using Postman I am trying to do the same thing using ESP32. I want to upload an audio file from the SD card connected with my ESP32 and a simple text with it. But I don't know how to post the audio file using HTTP and how to combine the text with the audio file in the same post request. Can anyone help? Previously I sent some JSON data from ESP to the server and it was quite easy using HttpClient and ArduinoJSON library.

1 Answers1

2

you can use this code for sending form-data to http server: FirstConfigNameProj is text data and fb is our image file.

const char *server = "testhost.ir"; // Server URL
if (!client.connect(server, 80))
    Serial.println("Connection failed!");
else
{
    String serverPath1 = "http://testhost.ir/api/Camera";
    String serverName1 = "testhost.ir";

    Serial.println("Connection successful!" + enid);
    String bound = "boundry";
    String FirstConfigNameProj = "--" + bound + "\r\nContent-Disposition: form-data; name=\"fkProject\"" + "\r\n\r\n" + String(prjNo) + "\r\n";

    String head = "--" + bound + "\r\nContent-Disposition: form-data; name=\"cameraImage\";filename=\"IMAGE19.JPG\"\r\nContent-Type: image/jpeg\r\n\r\n";
    String tail = "\r\n--" + bound + "--\r\n";

    uint32_t imageLen = fb->len;
    uint32_t extraLen = head.length() + tail.length();
    uint32_t totalLen = imageLen + extraLen;
    Serial.println("first step");
    client.println("POST " + serverPath1 + " HTTP/1.1");
    client.println("Host: " + serverName1);

    // content length
    uint32_t contentLength = FirstConfigNameProj.length() + totalLen;

    // send post header

    client.println("Content-Length: " + String(contentLength));
    client.println("Content-Type: multipart/form-data; boundary=" + bound);
    client.println();
    char charBufKey[FirstConfigNameProj.length() + 1];
    FirstConfigNameProj.toCharArray(charBufKey, FirstConfigNameProj.length() + 1);
    client.write(charBufKey);
    client.println();
    client.print(head);
    Serial.println("second step");
    uint8_t *fbBuf = fb->buf;
    size_t fbLen = fb->len;
    for (size_t n = 0; n < fbLen; n = n + 1024)
    {
        if (n + 1024 < fbLen)
        {
            client.write(fbBuf, 1024);
            fbBuf += 1024;
        }
        else if (fbLen % 1024 > 0)
        {
            size_t remainder = fbLen % 1024;
            client.write(fbBuf, remainder);
        }
    }
    client.print(tail);

    esp_camera_fb_return(fb);

    int timoutTimer = 10000;
    long startTimer = millis();
    boolean state = false;

    Serial.println("third step");
    while ((startTimer + timoutTimer) > millis())
    {
        Serial.print(".");
        delay(100);
        while (client.available())
        {
            char c = client.read();
            if (c == '\n')
            {
                if (getAll.length() == 0)
                {
                    state = true;
                }
                getAll = "";
            }
            else if (c != '\r')
            {
                getAll += String(c);
            }
            if (state == true)
            {
                getBody += String(c);
            }
            startTimer = millis();
        }
        if (getBody.length() > 0)
        {
            break;
        }
    }
    client.stop();
    Serial.println(getBody);
}