Send images from the SPIFF with a POST request to a server
Posted: Mon Jul 01, 2019 10:03 am
What I'm trying: trying to send images (using POST requests) to a server that will look for certain items in the image and returns a JSON object with info I need.
What I have working: I have everything working with a POST request to the API from the server using Base64 string images manually converted, the JSON body is getting nicely parsed and I get all the data I need using the following code.
What I want: send an image FILE in the POST request directly from the ESP8266 SPIFF system, as said before now I manually convert them to a base64 string which is not what I want. I've googled a a lot but I can't find any example on this, and I really have no idea where to start or what to do. Here is what I have now after lots of trying:
The commented code are parts which I've tried with and without and no clue if they are really needed. I have a strong feeling that I need to read byte by byte from the image and stream it or somehow, but I have really no clue how to or where to start.
With the current code my output is:
[ode]{"error_code": 400, "error": "Missing file named \"image\" in request POST"}[/code]
I got the output from listening to the client and directly printing in the serial monitor, which should return the JSON body with all the info I want.
As an example this is how you would do it in the CMD prompt (this one is for the base64 images, which I got working):
Which uses the Base64, I got this one working
Now the other one is for the file images, which I haven't got working (these post requests are just to give you an idea of how this API is working!):
I hope anyone can help me.
What I have working: I have everything working with a POST request to the API from the server using Base64 string images manually converted, the JSON body is getting nicely parsed and I get all the data I need using the following code.
Code: Select all
httpsClient.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Content-Type: application/json" + "\r\n" +
"Content-Length: " + imageBase64.length() + "\r\n\r\n" +
imageBase64 + "\r\n" +
"Connection: close\r\n\r\n");
What I want: send an image FILE in the POST request directly from the ESP8266 SPIFF system, as said before now I manually convert them to a base64 string which is not what I want. I've googled a a lot but I can't find any example on this, and I really have no idea where to start or what to do. Here is what I have now after lots of trying:
Code: Select all
File imageFile;
imageFile = SPIFFS.open("/image.png", "r");
httpsClient.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
//"Content-Type: multipart/form-data" + "\r\n" +
"Content-Type: image/png" + "\r\n" +
"Content-Disposition: form-data; name=\"image\"; filename=\"image.png\"" + "\r\n" +
//"Content-Transfer-Encoding: binary" + "\r\n" +
"Content-Length: " + imageFile.size() + "\r\n\r\n");
imageFile + "\r\n" +
"Connection: close\r\n\r\n");
The commented code are parts which I've tried with and without and no clue if they are really needed. I have a strong feeling that I need to read byte by byte from the image and stream it or somehow, but I have really no clue how to or where to start.
With the current code my output is:
[ode]{"error_code": 400, "error": "Missing file named \"image\" in request POST"}[/code]
I got the output from listening to the client and directly printing in the serial monitor, which should return the JSON body with all the info I want.
As an example this is how you would do it in the CMD prompt (this one is for the base64 images, which I got working):
Code: Select all
curl -X POST "https://api.openalpr.com/v2/recognize_bytes?secret_key=YOUR_TOKEN&recognize_vehicle=0&country=eu&return_image=0&topn=1" -H "accept: application/json" -H "Content-Type: application/json" -d "YOUR_IMAGE_BYTES"
Which uses the Base64, I got this one working
Now the other one is for the file images, which I haven't got working (these post requests are just to give you an idea of how this API is working!):
Code: Select all
curl -X POST "https://api.openalpr.com/v2/recognize?secret_key=YOUR_TOKEN&recognize_vehicle=0&country=eu&return_image=0&topn=1" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F image=@C:\Users\Name\Desktop\image.png;type=image/png
I hope anyone can help me.