I’m trying to hit an OCR API using Al code, and while it works fine in Postman, it's throwing an error in Al. The OCR API team confirmed everything is fine on their end, suggesting an issue with my Al code.
I’ve attached screenshots of both Postman screenshots and the Al code for reference. Could anyone help identify what might be wrong?
{
actions
{
addbefore(Vendor)
{
action("Upload PO PDF")
{
ApplicationArea = All;
Caption = 'Upload Purchase Order';
Enabled = true;
ToolTip = 'Upload and process Purchase Order using OCR';
Visible = true;
Image = Import;
Promoted = true;
PromotedCategory = Process;
var
TempBlob: Codeunit "Temp Blob";
FileInStream: InStream;
FileName: Text;
DialogTitle: Label 'Select a PDF file';
OCRAPICall: Codeunit "OCR API Call";
StatusCode: Integer;
begin
if UploadIntoStream(DialogTitle, '', 'PDF Files (*.pdf)|*.pdf', FileName, FileInStream) then begin
TempBlob.CreateInStream(FileInStream);
StatusCode := OCRAPICall.UploadFileToOCR(TempBlob, FileName);
Message('File uploaded. Status Code: %1', StatusCode);
end else
Message('No file selected.');
end;
}
}
}
}
codeunit 71516705 "OCR API Call"
{
procedure UploadFileToOCR(var TempBlob: Codeunit "Temp Blob"; FileName: Text): Integer
var
Client: HttpClient;
RequestMessage: HttpRequestMessage;
ResponseMessage: HttpResponseMessage;
Content: HttpContent;
Headers: HttpHeaders;
InStr: InStream;
Boundary: Text;
ResponseText: Text;
TempBlobOutStr: OutStream;
FileContent: Text;
begin
Boundary := '123456789';
Content.WriteFrom('--' + Boundary + '\r\n');
Content.WriteFrom('Content-Disposition: form-data; name="file"; filename="' + FileName + '"\r\n');
Content.WriteFrom('Content-Type: application/pdf\r\n\r\n');
TempBlob.CreateOutStream(TempBlobOutStr);
TempBlob.CreateInStream(InStr);
CopyStream(TempBlobOutStr, InStr);
TempBlob.CreateInStream(InStr);
Content.WriteFrom(InStr);
Content.GetHeaders(Headers);
if Headers.Contains('Content-Type') then
Headers.Remove('Content-Type');
Headers.Add('Content-Type', 'multipart/form-data; boundary="' + Boundary + '"');
RequestMessage.SetRequestUri('https://api.ocr.space/parse/image');
RequestMessage.Method := 'POST';
RequestMessage.Content := Content;
RequestMessage.GetHeaders(Headers);
Headers.Add('apikey', 'K89917254588957');
Client.Send(RequestMessage, ResponseMessage);
ResponseMessage.Content.ReadAs(ResponseText);
Message('OCR Response: %1', ResponseText);
exit(ResponseMessage.HttpStatusCode());
end;
}
reference: Uploading a file from MSDyn365BC as "multipart/form-data" message - Never Stop Learning (never-stop-learning.de)
OCR Response: {"OCRExitCode":99,"IsErroredOnProcessing":true,"ErrorMessage":["Unable to recognize the file type","E216: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"}
Any help would be appreciated. Thanks !!!