post - 如何将多部分/表单数据(文件和文本)从 ESP32 发布到服务器?

标签 post postman multipartform-data esp32 arduino-esp32

我正在尝试从 ESP32 将音频文件和文本发布到服务器。我可以使用 Postman 轻松完成此类发布请求。 Post request using Postman 我正在尝试使用 ESP32 做同样的事情。我想从与 ESP32 连接的 SD 卡上传一个音频文件和一个简单的文本。但我不知道如何使用 HTTP 发布音频文件以及如何在同一个发布请求中将文本与音频文件结合起来。有人可以帮忙吗? 之前我从 ESP 向服务器发送了一些 JSON 数据,使用 HttpClient 和 ArduinoJSON 库非常简单。

最佳答案

您可以使用此代码将表单数据发送到http服务器:FirstConfigNameProj是文本数据,fb是我们的图像文件。

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);
}

关于post - 如何将多部分/表单数据(文件和文本)从 ESP32 发布到服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68115055/

相关文章:

node.js - 在 NodeJS 中运行 Postman Newman 时通过测试修改环境

Java 多部分帖子

c++ - 使用 libcurl 发送 post 请求

php - 图片/文件上传不适用于 materializecss 框架

javascript - 是否可以使用 PHP 表单传递 html 元素属性?

botframework - 400 来自使用 Postman 的本地机器人的错误请求

javascript - Postman:如何在请求正文中使用环境变量

amazon-web-services - s3 预签名 url 多部分表单数据上传错误 :signature does not match

Express JS 模块解析来自 Postman 的表单数据文本

android - 使用 retrofit2 和 rx java2 发送大量 POST 时出现 OutOfMemoryException