web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

OCR API works in Postman but not in my AI code

(1) ShareShare
ReportReport
Posted on by 157
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 !!! 









 
I have the same question (0)
  • Verified answer
    Gerardo Rentería García Profile Picture
    25,390 Most Valuable Professional on at
    Hi
    I'm sorry, but I don't see the error in AL, can you tell us what error you get and where?
    Best
    GR

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 1,856

#2
Khushbu Rajvi. Profile Picture

Khushbu Rajvi. 765 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 581 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans