Hi community,
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?
pageextension 71516708 "Purchase Order Ext OCR" extends "Purchase Invoice"
{
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;
trigger OnAction()
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';
// Set up the message content for multipart/form-data
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');
// Read the file content into a temporary blob
TempBlob.CreateOutStream(TempBlobOutStr);
TempBlob.CreateInStream(InStr);
CopyStream(TempBlobOutStr, InStr);
// Append the file content to the HttpContent
TempBlob.CreateInStream(InStr);
Content.WriteFrom(InStr);
Content.WriteFrom('\r\n--' + Boundary + '--\r\n');
// Get Content Headers
Content.GetHeaders(Headers);
// Update the content header information and define the boundary
if Headers.Contains('Content-Type') then
Headers.Remove('Content-Type');
Headers.Add('Content-Type', 'multipart/form-data; boundary="' + Boundary + '"');
// Setup the request
RequestMessage.SetRequestUri('https://api.ocr.space/parse/image');
RequestMessage.Method := 'POST';
RequestMessage.Content := Content;
// Add the API key to the request headers
RequestMessage.GetHeaders(Headers);
Headers.Add('apikey', 'K89917254588957');
// Send the message
Client.Send(RequestMessage, ResponseMessage);
// Get the response content
ResponseMessage.Content.ReadAs(ResponseText);
Message('OCR Response: %1', ResponseText);
// Return the Status Code
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 !!!