arduino-libraries / ArduinoHttpClient

Arduino HTTP Client library
288 stars 172 forks source link

How to POST a file #67

Closed abhiking-agrawal closed 5 years ago

abhiking-agrawal commented 5 years ago

I am looking for uploading a file to backend system.

Please do let me know if its possible with this.

Thanks in advance.

sandeepmistry commented 5 years ago

Hi @abhiking-agrawal,

Yes, it's possible to post a file. You'll have to use the appropriate content type and body format that the server expects.

See the PostWithHeaders example: https://github.com/arduino-libraries/ArduinoHttpClient/blob/master/examples/PostWithHeaders/PostWithHeaders.ino#L57-L64

You'll have to change the client.print(postData); line to Stream the file contents.

rtarta commented 4 years ago

@sandeepmistry Hi when i use below code i get Status code: -3 this is espcam32 camera image send sample of my code.
_sizepicture: 160330 fb is my espcam32 image buffer.

uint8_t* imagedata =  fb->buf;
size_t size_picture=fb->len;

HttpClient http(client, server, 80);
  http.connectionKeepAlive();
  Serial.println("making POST request");

  Serial.print("size_picture: ");
  Serial.println(size_picture);

  http.beginRequest();
  http.post(resource);
  http.sendHeader("Content-Type", "application/octet-stream");
  http.sendHeader("Content-Length", size_picture);
  http.sendHeader("X-FileName", "FileNameXXXX");
  http.beginBody();
  http.write(imagedata ,size_picture);  
  http.endRequest();

  // read the status code and body of the response
  int statusCode = http.responseStatusCode();
  String response = http.responseBody();

  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.print("Response: ");
  Serial.println(response);