I’m a DIYer and I’m trying to upload a picture from ESP32-CAM to imgbb.com using POST. This project from RandomNerdTutorials gave me a better notion about POST request on C++.
I took their code and modified it to upload to 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 = "--RandomNerdTutorialsrnContent-Disposition: form-data; name="photo.jpg"; filename="photo.jpg"rnContent-Type: image/jpegrnrn";
// String head = "--RandomNerdTutorialsrnContent-Disposition: form-data; name="photo.jpg"; filename="photo.jpg"rnrn";
String tail = "rn--RandomNerdTutorials--rn";
uint16_t imageLen = fb->len;
uint16_t extraLen = head.length() + tail.length();
uint16_t totalLen = imageLen + extraLen;
Serial.printf("imageLen:%d - extraLen: %d - totalLen: %dn", 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: %dn", fbLen);
int count = 0;
for (size_t n=0;n<fbLen;n=n+1024) {
count++;
Serial.printf("Sending part %dn", 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;
}
I get this on Serial Monitor
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"}
In case you are asking for the cURL example output on their API site, the output using -v
option prints:
With command
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, X-Requested-With, Content-Type
< Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Methods: POST, GET, 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","name":"pull","mime":"image/jpeg","extension":"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
2
Answers
You only need to modify the parameter name of imageField to "image".
https://api.imgbb.com/
Parameters
image (required)
A binary file, base64 data, or a URL for an image. (up to 32 MB)