Skip to main content

Notifications

Announcements

No record found.

Business Central forum
Unanswered

OCR API works in Postman but not in my AI code

Posted on by 11
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 9,676 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

Quick Links

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,556 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,625 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans