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...
Suggested Answer

AL Converting Image to a Application/octet-stream

(0) ShareShare
ReportReport
Posted on by

My goal is to execute a SOAP API call and get a prediction from the Microsoft Azure Custom vision prediction API.

The documentation for the API call:

Set Prediction-Key Header to : someRandomGeneratedcode
Set Content-Type Header to : application/octet-stream
Set Body to : <image file>
My question is how can I convert an image to the content-type application/octet-stream?

Things I tried are:
Convert the image to a Base64String what resolves in the error badRequestImageFormat.
 
        lFile.Open(ptxtImagepath, TextEncoding::UTF16);

        lFile.CREATEINSTREAM(lisInStream);
        lnMemStream := lnMemStream.MemoryStream();
        COPYSTREAM(lnMemStream, lisInStream);
        lnArray := lnMemStream.GetBuffer();
        pbtxtImage.ADDTEXT(lnConvert.ToBase64String(lnArray));
        lFile.close();
use the dotnet Binaryreader like the following example. Which resolves in the following error: Unable to read beyond
 the end of the stream.

        lFile.Open(ptxtImagepath, TextEncoding::UTF8);

        lFile.CREATEINSTREAM(lisInStream);
        lnMemStream := lnMemStream.MemoryStream();
        COPYSTREAM(lnMemStream, lisInStream);

        lbrBinaryReader := lbrBinaryReader.BinaryReader(lnMemStream, lEncoding.UTF8());
        IF NOT ISNULL(lbrBinaryReaderTHEN BEGIN   //read value from BLOB field
            IF lbrBinaryReader.BaseStream().Length() 0 THEN
                ltxtBigText := lbrBinaryReader.ReadString();
            lbrBinaryReader.Close();
        END;
        //**********
        pbtxtImage.ADDTEXT(ltxtBigText);
        lFile.close();
I have the same question (0)
  • Suggested answer
    THE Italian Profile Picture
    on at

    If you cannot make it to resolve with C/AL within this outdated version (NAV 2013 is out of support since several years now), I would suggest that you create a .NET assembly that performs what is needed instead.

    Another path to investigate is upgrading to latest Business Central and make use of HttpClient data type calls

  • Suggested answer
    rkwadraat Profile Picture
    10 on at

    Put the blob directly in an byte array, and use the bytearray in the request stream

    Sample:

    // Tempblob is containing the image or document or whatever
    // ByteArray = System.Array from 'mscorlib'
    // first fill the byte array
    TempBlob.Blob.CREATEINSTREAM(InStream);
    MemoryStream := MemoryStream.MemoryStream; 
    COPYSTREAM( MemoryStream, InStream); 
    ByteArray := MemoryStream.ToArray();
    BytesRead := ByteArray.Length;
    ...
    ...
    // use the request stream of the httprequest and put in the bytearray:
    RequestStream := HttpWebRequest.GetRequestStream;
    RequestStream.Write( ByteArray, 0, ByteArray.Length);

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 3,143

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,694 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,067 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans