Page 1 of 1

POST request with PHOTO file in the body of the message. How to make ?

Posted: Fri Apr 21, 2023 2:40 pm
by esp@esp@esp
Greetings.

The relevant part of my code is this, already well known, which sends a photo from esp32cam to a website, server:

Code: Select all

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

  if (client.connect(host.c_str(), serverPort))  {

    Serial.println("-- CONNECTED --");    

    String head = "--testeboundary\r\nContent-Disposition: form-data;\r\n{\r\n\"image\":\"photo\",\r\n\"identifier\":\"Peter\",\r\n\"group_name\":\"A\"\r\n} \r\n";
    String tail = "\r\n--testeboundary--\r\n";

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

    client.println("POST " + url + " HTTP/1.1");
    client.println("Host: " + host);
    client.println("X-RapidAPI-Key:f24a8bec96msh336f26930e2c623p10fc12");
    client.println("X-RapidAPI-Host:xbotman-files.p.rapidapi.com"),
    client.println("Content-Length: " + String(totalLen));
    client.println("Content-Type: multipart/form-data; boundary=--testeboundary");
    client.println();
    client.print(head);
  
    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);
    


I'm testing an API, and I was successful in sending a photo through Postman. I have attached here the code example in Ruby - Net::HTTP as it is similar to the C++ used in the Arduino IDE. Unfortunately there is no example in C++:

Code: Select all

require "uri"
require "net/http"

url = URI("https://.....................rapidapi.com/register")

https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true

request = Net::HTTP::Post.new(url)
request["X-RapidAPI-Key"] = "f24a8bec96msh336f26930e2c623p10fc12"
request["X-RapidAPI-Host"] = "xbotman-.p.rapidapi.com"
form_data = [['image', File.open('6wkop7Or4/RostoA.jpg')],['identifier', 'Alberto'],['group_name', 'A']]
request.set_form form_data, 'multipart/form-data'
response = https.request(request)
puts response.read_body

I'm not a good programmer. With my code, the file photo goes outside the body of the message, and the API returns error 1001 which means image not found. Note that in Postman's code the photo file was placed in form_data = [['image', File.open('6wkop7Or4/RostoA.jpg')]...

That's my problem. I can't do that inside the String Head because the photo is already being sent by another path.

After much research, I did several tests with several different ways but without success. And I know that it can often just be a detail.

Does anyone know what I could try ? How could that relevant snippet of my code be used to successfully send the photo ?

Grateful.

Re: POST request with PHOTO file in the body of the message. How to make ?

Posted: Fri Apr 21, 2023 10:07 pm
by esp@esp@esp
Or else, can someone post a link that may have some example code different from mine? The camera output has to go in the body of the request

Thanks

Re: POST request with PHOTO file in the body of the message. How to make ?

Posted: Mon Apr 24, 2023 8:00 am
by a2800276
I think it might be easier to use an existing HTTP client library like https://github.com/amcewen/HttpClient rather than implementing the intricacies of POST requests yourself. It would certainly save a lot of code.

Re: POST request with PHOTO file in the body of the message. How to make ?

Posted: Mon Apr 24, 2023 10:46 am
by esp@esp@esp
And truth. I had already tried HttpClient at the beginning of my journey. I will try again with her now encouraged by this good suggestion of yours.

Thanks