Skip to main content

Notifications

Small and medium business | Business Central, N...
Unanswered

OCR API works in Postman but not in my AI code

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









 
  • gdrenteria Profile Picture
    gdrenteria 12,290 Most Valuable Professional on at
    OCR API works in Postman but not in my AI code
    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

News and Announcements

Announcing Category Subscriptions!

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,359 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,370 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans