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

How to access custom api in AL Code using On Premise business central?

(1) ShareShare
ReportReport
Posted on by 274
Hello Sir/Ma'am,

I have consume the custom api in postman using /NTLM authentication/ for Business central On-premise
Now, I want to call that api through al code here is the al code :
 
procedure CallAPI()
    var
        Content: HttpContent;
        ContentHeaders: HttpHeaders;
        IsSuccessful: Boolean;
        HttpClient: HttpClient;
        ResponseMessage: HttpResponseMessage;
        JToken: JsonToken;
        JObject: JsonObject;
        JValue: JsonValue;
        RequestHeaders: HttpHeaders;
        RequestMessage: HttpRequestMessage;
        ResponseText: Text;
    begin
        // HttpClient.UseWindowsAuthentication('username', 'password');
        Content.GetHeaders(contentHeaders);
        RequestMessage.GetHeaders(RequestHeaders);
        RequestHeaders.Add('Username', 'username');
        RequestHeaders.Add('Password', 'password');
        // ContentHeaders.Add('Username', 'username');
        // ContentHeaders.Add('Password', 'password');
        // RequestHeaders.Add('request-id', Format(Random(9999)));
        // RequestHeaders.Add('OData-Version', '4.0');
        // RequestHeaders.Add('Access-Control-Allow-Credentials', 'true');
        // RequestMessage.SetRequestUri(Rec./API URL/);
        RequestMessage.SetRequestUri('http://localhost:7048/BC220/ODataV4/Company(''CRONUS%20India%20Ltd.'')/CustomAPI');
        RequestMessage.Method('GET');
 
        IsSuccessful := HttpClient.Send(RequestMessage, ResponseMessage);
        if not IsSuccessful then
            Error('Status Code: %1// Reason : %2', ResponseMessage.HttpStatusCode, ResponseMessage.ReasonPhrase)
        else begin
            ResponseMessage.Content.ReadAs(ResponseText);
            JObject.ReadFrom(ResponseText);
            Message(ResponseText);
        end;
    end;
 
It's giving the error as 401 Unauthorized 
please help on how to consume custom api  through ntlm authentication
Your efforts are appreciated.
Thank you for your help
  • Shrey Chauhan Profile Picture
    274 on at
    How to access custom api in AL Code using On Premise business central?
    Hello YUN ZHU Sir,

    I have tried the code as you suggested in your response reference but still it can't authenticate 
    Here's revised code :
    procedure CallAPI()
        var
            RequestText: Text;
            AuthText: Text;
            Base64Text: Text;
            Convert: Codeunit "Base64 Convert";
            Content: HttpContent;
            ContentHeaders: HttpHeaders;
            IsSuccessful: Boolean;
            HttpClient: HttpClient;
            ResponseMessage: HttpResponseMessage;
            JToken: JsonToken;
            JObject: JsonObject;
            JValue: JsonValue;
            RequestHeaders: HttpHeaders;
            RequestMessage: HttpRequestMessage;
            ResponseText: Text;
            TempBlob: Codeunit "Temp Blob";
        begin
            Content.WriteFrom(RequestText);
            Content.GetHeaders(ContentHeaders);
            RequestMessage.Content := Content;
            RequestMessage.GetHeaders(RequestHeaders);
            RequestMessage.SetRequestUri('http://localhost:7048/BC220/ODataV4/Company(''CRONUS%20India%20Ltd.'')/CustomAPI');
            RequestMessage.Method('GET');
            AuthText := strsubstNo('%1:%2', 'username', 'password');
            Base64Text := Convert.ToBase64(AuthText); // Convert text to Base64
            RequestHeaders.Add('Authorization', StrSubstNo('Basic %1', Base64Text));
            RequestHeaders.Add('Connection', 'keep-alive');
            HttpClient.Timeout := 30000;
            HttpClient.send(RequestMessage, ResponseMessage);
        end;

    Please help me to solve this error 
    Thank you for your time and response.
     
  • Suggested answer
    YUN ZHU Profile Picture
    81,873 Super User 2025 Season 1 on at
    How to access custom api in AL Code using On Premise business central?
    Hi, hopefully the information below will give you some tips.
     
     
    Thanks
    ZHU

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,312 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 233,025 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans