ERROR when making POST request of base64 photo to free API using arduino ESP32

Hi Class. I am new here. I’m using arduino ESP32cam and I managed to make a GET request and see that the OCR system really works.

However, my arduino C++ IDE code generates the base64 String of every photo I take with the ESP32cam.

There are no such examples in the tutorials. The examples deal with photos saved to URLs. It’s not my case. I have the photo already in base64 String inside my code.

I tried to send it using the code below. But it returns this error: “OCRExitCode”:99,“IsErroredOnProcessing”:true,“ErrorMessage”:[“Unable to recognize the file type”,“Unable to detect the file extension, or the file extension is incorrect, and no ‘file type’ provided in request. Please provide a file with a proper content type or extension, or provide a file type in the request to manually set the file extension.”],“ProcessingTimeInMilliseconds”:“0”}

Can anyone explain how to resolve this ? Thanks

HTTPClient http;   
  
http.begin("http://api.ocr.space/parse/image");
http.addHeader("apikey:", "A85066677886547");
http.addHeader("Content-Type", "image/jpg"); 
 
String data = "data:image/jpeg;base64,";
String httpRequestData = data + urlencode(photo);

Serial.println(httpRequestData);

int httpResponseCode = http.POST(httpRequestData);

With or without urlencode(photo), the error is the same. The base64 String is photo.

And when I read Serial.println(httpRequestData); I notice that it is the same as the example of the tutorials on the site. So I can’t understand why it doesn’t work.

Maybe there is an error in the created BASE64 string? I see two options to debug this:

Take the string, and copy it (on your PC) into Postman. =>If you test the connection with Postman, do you get the same result?

You probably know it, but just in case: Postman is a free cross-platform app.

If you see the problem even with Postman, a screenshot of the result could be helpful.

Or, in your C++ code, add one of these know valid OCR API base64 image strings. Does the ocr work then?

Ah, I see the the issue: To use BASE64 image strings, you need to make a POST request (not GET!):

The important limitation of the GET api endpoint is it only allows image and PDF submissions via the URL method, as only HTTP POST requests can supply additional data to the server in the message body. GET requests include all required data in the URL. So by design, a GET api cannot support file uploads (file parameter) or BASE64 strings (base64image ).

We have some OCR C++ sample code. Or you can use Postman to generate C code:

That. I am only able to successfully perform GET requests. That is, with URL’s images.

When I try to make POST requests using an image already encoded within my code, I get an error.

Is the google search button as used as a valid example described in the API.

The final send String looks like this:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAA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Zx5r/3ua/N1ve+oSSjUqqGkPaT5bbcrd/d6Wu3a2nY7vNZgNtf+PiT/rnH/wChS0AadAFDUv8AUj/rpF/6NSgCLOKADOKADOKADOKADOKAOYvPB2k39099PAWmlMRlAlmSKYwnMRngSRYJzGcbDNG+MDHQYI/u3eOlpcy7KTVuaKekZeas763vqEveVpfyuPZ8r3i2tXF3d09Gm1sdPnFABnFABnFABnFABnFABnFABnFABnFABnFABaf8fEn/AFzj/wDQpaANSgCvdQG4jKA7TlSD7qwYfhkDNAGf9ku/78X/AHw3/wAcoAPsl3/fi/74b/45QAfZLv8Avxf98N/8coAPsl3/AH4v++G/+OUAH2S7/vxf98N/8coAPsl3/fi/74b/AOOUAH2S7/vxf98N/wDHKAD7Jd/34v8Avhv/AI5QAfZLv+/F/wB8N/8AHKAD7Jd/34v++G/+OUAH2S7/AL8X/fDf/HKAD7Jd/wB+L/vhv/jlAB9ku/78X/fDf/HKAD7Jd/34v++G/wDjlAB9ku/78X/fDf8AxygC1Z2zws0kpDMwUfKMABckcEk5yx70AXqACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKAP/2Q==  

Looks ok => Does it work in Postman?

I’ll test it and come back here to confirm. But let’s assume it works there. My code is generating the same String. I don’t understand. But like I said, I’ll confirm and come back here to say.

I was curious and tested it myself. All works fine in Postman. So the string is OK, but maybe something wrong with the Arduino C++ POST code?

Base64 OCR:

Something seems wrong in the C++ code is what motivated me to make the first post here in this forum. Who knows, maybe someone has gone through this before. But it seems not. I’ll keep searching. Thanks.

Enjoying that I’m still here. Question: Is there any way to handle the response event. I mean, the response code. Eliminate fields leaving only the field we want in the String response ?

{"ParsedResults":[{"TextOverlay":{"Lines":[],"HasOverlay":false,"Message":"Text overlay is not provided as it is not requested"},"TextOrientation":"0","FileParseExitCode":1,"ParsedText":"PR • CORTIYU\r\nBC- 1234\r\n","ErrorMessage":"","ErrorDetails":""}],"OCRExitCode":1,"IsErroredOnProcessing":false,"ProcessingTimeInMilliseconds":"671","SearchablePDFURL":"Searchable PDF not generated as it was not requested."}