如何在ESP32 CAM上将图像发布到imgbb.com?我得到“空上传源”

问题描述

我是DIYer,正在尝试使用POST将图片从ESP32-CAM上传到imgbb.com。来自RandomNerdTutorials的This project使我对C ++上的POST请求有了更好的理解。

我获取了他们的代码,并将其修改为上传到https://api.imgbb.com

/*
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp32-cam-shield-pcb-telegram/
  
  Project created using Brian Lough's Universal Telegram Bot Library: https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*/
// Partial code modified to upload to imgbb.com
String uploadPhoto(char *apikey){
  String getAll = "";
  String getBody = "";

  camera_fb_t * fb = NULL;
  fb = esp_camera_fb_get();  
  if(!fb) {
    Serial.println("Camera capture failed");
    delay(1000);
    ESP.restart();
    return "Camera capture failed";
  }
  
  Serial.println("Connect to api.imgbb.com");

  if (clientTCP.connect("api.imgbb.com",443)) {
    Serial.println("Connection successful");
    
    String head = "--RandomNerdTutorials\r\nContent-Disposition: form-data; name=\"photo.jpg\"; filename=\"photo.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n";
    // String head = "--RandomNerdTutorials\r\nContent-Disposition: form-data; name=\"photo.jpg\"; filename=\"photo.jpg\"\r\n\r\n";
    String tail = "\r\n--RandomNerdTutorials--\r\n";

    uint16_t imageLen = fb->len;
    uint16_t extraLen = head.length() + tail.length();
    uint16_t totalLen = imageLen + extraLen;

    Serial.printf("imageLen:%d - extraLen: %d - totalLen: %d\n",imageLen,extraLen,totalLen);
  
    clientTCP.println("POST /1/upload?expiration=600&key="+String(apikey)+" HTTP/1.1");
    clientTCP.println("Host: api.imgbb.com");
    clientTCP.println("Content-Length: " + String(totalLen));
    clientTCP.println("Content-Type: multipart/form-data; boundary=RandomNerdTutorials");
    clientTCP.println();
    clientTCP.print(head);
  
    uint8_t *fbBuf = fb->buf;
    size_t fbLen = fb->len;
    Serial.printf("fbLen: %d\n",fbLen);
    int count = 0;
    for (size_t n=0;n<fbLen;n=n+1024) {
      count++;
      Serial.printf("Sending part %d\n",count);
      if (n+1024<fbLen) {
        clientTCP.write(fbBuf,1024);
        fbBuf += 1024;
      }
      else if (fbLen%1024>0) {
        size_t remainder = fbLen%1024;
        clientTCP.write(fbBuf,remainder);
      }
    }
    
    clientTCP.print(tail);
    
    esp_camera_fb_return(fb);
    
    unsigned long now = millis();
    boolean state = false;
    
    while (millis() - now < 10000){ // timeout 10 seconds
      Serial.print(".");
      delay(100);      
      while (clientTCP.available()){
          char c = clientTCP.read();
          if (c == '\n'){
            if (getAll.length()==0) state=true; 
            getAll = "";
          } 
          else if (c != '\r'){
            getAll += String(c);
          }
          if (state==true){
            getBody += String(c);
          }
       }
       if (getBody.length()>0) break;
    }
    clientTCP.stop();
    Serial.println(getBody);
  }
  else {
    getBody="Connected to api.imgbb.com failed.";
    Serial.println("Connected to api.imgbb.com failed.");
  }
  return getBody;
}

我在串行监视器上得到了

Connection successful
imageLen:7522 - extraLen: 150 - totalLen: 7672
fbLen: 7522
Sending part 1
Sending part 2
Sending part 3
Sending part 4
Sending part 5
Sending part 6
Sending part 7
Sending part 8
..
7a
{"status_code":400,"error":{"message":"Empty upload source.","code":130,"context":"Exception"},"status_txt":"Bad Request"}

如果您要在其API站点上请求cURL示例输出,则使用-v选项的输出将输出: 使用命令

curl --location --request POST "https://api.imgbb.com/1/upload?expiration=600&key=YOUR_CLIENT_API_KEY" --form image=@"example.jpg" -v
Note: Unnecessary use of -X or --request,POST is already inferred.
*   Trying 51.81.57.189...
* TCP_NODELAY set
* Connected to api.imgbb.com (51.81.57.189) port 443 (#0)
* schannel: SSL/TLS connection with api.imgbb.com port 443 (step 1/3)
* schannel: checking server certificate revocation
* schannel: sending initial handshake data: sending 178 bytes...
* schannel: sent initial handshake data: sent 178 bytes
* schannel: SSL/TLS connection with api.imgbb.com port 443 (step 2/3)
* schannel: failed to receive handshake,need more data
* schannel: SSL/TLS connection with api.imgbb.com port 443 (step 2/3)
* schannel: encrypted data got 3525
* schannel: encrypted data buffer: offset 3525 length 4096
* schannel: sending next handshake data: sending 93 bytes...
* schannel: SSL/TLS connection with api.imgbb.com port 443 (step 2/3)
* schannel: encrypted data got 51
* schannel: encrypted data buffer: offset 51 length 4096
* schannel: SSL/TLS handshake complete
* schannel: SSL/TLS connection with api.imgbb.com port 443 (step 3/3)
* schannel: stored credential handle in session cache
> POST /1/upload?expiration=600&key=1be33137765dd807cb05b2ff94885779 HTTP/1.1
> Host: api.imgbb.com
> User-Agent: curl/7.55.1
> Accept: */*
> Content-Length: 18608
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------c67a7796e0413135
>
* schannel: client wants to read 102400 bytes
* schannel: encdata_buffer resized 103424
* schannel: encrypted data buffer: offset 0 length 103424
* schannel: encrypted data got 54
* schannel: encrypted data buffer: offset 54 length 103424
* schannel: decrypted data length: 25
* schannel: decrypted data added: 25
* schannel: decrypted data cached: offset 25 length 102400
* schannel: encrypted data buffer: offset 0 length 103424
* schannel: decrypted data buffer: offset 25 length 102400
* schannel: schannel_recv cleanup
* schannel: decrypted data returned 25
* schannel: decrypted data buffer: offset 0 length 102400
< HTTP/1.1 100 Continue
HTTP/1.1 100 Continue

* schannel: client wants to read 102400 bytes
* schannel: encrypted data buffer: offset 0 length 103424
* schannel: encrypted data got 1096
* schannel: encrypted data buffer: offset 1096 length 103424
* schannel: decrypted data length: 1067
* schannel: decrypted data added: 1067
* schannel: decrypted data cached: offset 1067 length 102400
* schannel: encrypted data buffer: offset 0 length 103424
* schannel: decrypted data buffer: offset 1067 length 102400
* schannel: schannel_recv cleanup
* schannel: decrypted data returned 1067
* schannel: decrypted data buffer: offset 0 length 102400
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Server: nginx
Server: nginx
< Date: Mon,10 Aug 2020 20:08:42 GMT
Date: Mon,10 Aug 2020 20:08:42 GMT
< Content-Type: application/json; charset=UTF-8
Content-Type: application/json; charset=UTF-8
< Transfer-Encoding: chunked
Transfer-Encoding: chunked
< Connection: keep-alive
Connection: keep-alive
< Vary: Accept-Encoding
Vary: Accept-Encoding
< Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: *
< Access-Control-Allow-Headers: Cache-Control,X-Requested-With,Content-Type
Access-Control-Allow-Headers: Cache-Control,Content-Type
< Access-Control-Allow-Methods: POST,GET,OPTIONS
Access-Control-Allow-Methods: POST,OPTIONS
< Last-Modified: Mon,10 Aug 2020 20:08:42GMT
Last-Modified: Mon,10 Aug 2020 20:08:42GMT
< Cache-Control: no-cache,must-revalidate
Cache-Control: no-cache,must-revalidate
< Pragma: no-cache
Pragma: no-cache

<
{"data":{"id":"P6Ynw91","title":"pull","url_viewer":"https:\/\/ibb.co\/P6Ynw91","url":"https:\/\/i.ibb.co\/d5cqGLk\/pull.jpg","display_url":"https:\/\/i.ibb.co\/d5cqGLk\/pull.jpg","size":18421,"time":"1597090122","expiration":"600","image":{"filename":"pull.jpg","name":"pull","mime":"image\/jpeg","extension":"jpg","url":"https:\/\/i.ibb.co\/d5cqGLk\/pull.jpg"},"thumb":{"filename":"pull.jpg","url":"https:\/\/i.ibb.co\/P6Ynw91\/pull.jpg"},"delete_url":"https:\/\/ibb.co\/P6Ynw91\/7912eb21dadf18bcf78119d10ae7b427"},"success":true,"status":200}* Connection #0 to host api.imgbb.com left intact

解决方法

[]interface{}
,

您只需要将imageField的参数名称修改为“ image”。

form-data; name=\"image\"; filename=\"photo.jpg\"

https://api.imgbb.com/

参数

图片(必填)

二进制文件,base64数据或图像的URL。 (最大32 MB)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...