从 esp32 cam 到后端服务器的 POST 请求,带有镜像文件

POST request from esp32 cam to backend server with image file

提问人:Charlie_57 提问时间:11/16/2023 更新时间:11/16/2023 访问量:22

问:

我想从 esp32 cam 模块发送 post 请求。我从 esp32-cam 拍摄了一张镜像,我的后端服务器是使用 docker 部署的。我想将该图像文件添加到我的帖子请求中。 我尝试从 esp32-cam 运行 GET 请求到后端服务器,它成功了。 但是当我尝试运行POST请求时,我收到错误:-

Camera capture Successful

[HTTPS] begin...

[HTTPS] POST...

Sending HTTP POST request to: https://majorproject-atimjqjr2q-uc.a.run.app

[HTTPS] POST... code: 405

有人可以帮我解决我的问题吗?

这是 esp32-cam 的功能:-

void captureAndSendImage() {
  // Capture an image
  camera_fb_t *fb = esp_camera_fb_get();
  if (!fb) {
    Serial.println("Camera capture failed");
    return;
  }
  Serial.println("Camera capture Successful");


  // Send the captured image to the server
  WiFiClientSecure *client = new WiFiClientSecure;
  if(client) {
    // set secure client with certificate
    client->setCACert(rootCACertificate);
    //create an HTTPClient instance
    HTTPClient https;

    //Initializing an HTTPS communication using the secure client
    Serial.print("[HTTPS] begin...\n");

    if (https.begin(*client, serverUrl)) {  // HTTPS
      Serial.print("[HTTPS] POST...\n");

    // Set the headers
    https.addHeader("Content-Type", "multipart/form-data");
    https.addHeader("Host", "majorproject-atimjqjr2q-uc.a.run.app");
    https.addHeader("Cache-Control", "no-cache");

    // Set the image file data
    https.addHeader("Content-Disposition", "form-data; name=\"image\"; filename=\"image.jpeg\"");
    https.addHeader("Content-Type", "image/jpeg");

    Serial.println("Sending HTTP POST request to: " + String(serverUrl));
    // Send the POST request with the image data
    int httpCode = https.POST(fb->buf, fb->len);

      if (httpCode > 0) {
      // HTTP header has been send and Server response header has been handled
       Serial.printf("[HTTPS] POST... code: %d\n", httpCode);
      // file found at server
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          // print server response payload
          String payload = https.getString();
          Serial.println(payload);
        }
      }
      else {
        Serial.printf("[HTTPS] POST... failed, error: %s\n", https.errorToString(httpCode).c_str());
      }
      https.end();
    }
  }
  else {
    Serial.printf("[HTTPS] Unable to connect\n");
  }
  Serial.println();
  Serial.println("Waiting before the next round...");
  delay(12000);

  // Release the frame buffer when done with it
  esp_camera_fb_return(fb);
}

这是后端代码的一部分:-

@app.route('/')
def hello_world():
    return 'Hello, new World!'

@app.route('/checkImage', methods=['POST'])
def get_image():
    # Getting Image
    print(request)
    image_file = request.form['image']

    return 'Image received'
ESP32系列

评论

0赞 Tim Roberts 11/16/2023
您没有正确执行表单数据标头。图像必须在单独的部分中发送,就像多部分 MIME 消息一样。你现在所拥有的只是用第二个覆盖第一个。您需要阅读有关格式的信息。Content-Typeform-data

答: 暂无答案