1

I'm trying to post the buffer generated by the ESP32 camera to my personal API service I have built with Node.JS. The post gets sent successfully. I have yet to test it on large image (higher resolution).

I'm using a FRAMESIZE_HQVGA, // 240x176 for my images

void postImage(uint8_t* buf, size_t len)
{
  // prepare JSON document
  DynamicJsonDocument doc(len);
  doc["buffer"] = buf;

  HTTPClient http;   

  // Serialize JSON document
  String json;
  serializeJson(doc, json);

  http.begin("http://192.168.0.147:3001/api/pictures");
  http.addHeader("Content-Type", "application/json");     

  int httpResponseCode = http.POST(json); 

  if (httpResponseCode > 0) {
    // response
    String response = http.getString();
    Serial.println(httpResponseCode);
    Serial.println(response); 
  } else {
    Serial.print("Error sending POST: ");
    Serial.println(httpResponseCode);
  }

  // disconnect
  http.end();
}

In my Node.JSside, I receive the buffer but I'm unable to de serialize it to an image. This i smy body:

{ buffer: '����' }

I'm wondering what I'm doing wrong here. Should the image buffer be sent in base64, and if so, how? I want to be able to transform the image buffer into a proper image on the API side.

Here's my how I try to transform the image buffer to a proper image (in my API controller):

    const imageBuffer = Buffer.from(req.body.buffer);
    const filename = `./resources/pictures/image_${Date.now()}.jpg`;

    fs.writeFile(filename, imageBuffer,function(err) {
        if (err) {
            console.log(err);
        } else {
            console.log(`Writing ${filename}`);
        }
    }); 
Johnny boy
  • 21
  • 1
  • is there a specific reason to use JSON? – Juraj Dec 08 '21 at 07:50
  • @Juraj No specific reason. Am I better off without it? – Johnny boy Dec 08 '21 at 07:57
  • for JSON you would have to encode it as text (base64 is common). but you can upload it as any other classic file upload in the history of the www – Juraj Dec 08 '21 at 08:04
  • JSON is a poor choice for large binary blobs. You would be better off just sending the raw buffer, with appropriate Content-Type (`application/octet-stream` if no other type is suitable) and Content-Length (the `len` parameter of `postImage()`). – Edgar Bonet Dec 08 '21 at 09:00
  • @EdgarBonet Tried what you said. The body is empty when I receive the request in the API :/ – Johnny boy Dec 08 '21 at 16:17
  • 1
    @Johnnyboy: In that case you should debug the problem. Is the body sent? Did you look at it with a packet sniffer? Is the Node.js server discarding that body? – Edgar Bonet Dec 08 '21 at 16:37
  • I'm currently looking at it with a packet sniffer, thank you for the idea! The request does get sent (I console log the request when i receive it, but the body is empty). So it's safe to say the api does see the request – Johnny boy Dec 08 '21 at 16:42
  • @EdgarBonet Just to confirm. In wireshark, I do see the buffer in the body. For some reason the express API only accepts JSON – Johnny boy Dec 08 '21 at 17:05
  • @EdgarBonet It works now. Thank you very much for the help! Really appreciate it! – Johnny boy Dec 08 '21 at 17:45
  • would be better to send it to nodejs as a form file upload to avoid all the extra encoding. – dandavis Dec 08 '21 at 18:42
  • @dandavis, but we are already talking about a binary upload – Juraj Dec 09 '21 at 05:49

0 Answers0