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

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

(1) ShareShare
ReportReport
Posted on by 284
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
I have the same question (0)
  • Suggested answer
    YUN ZHU Profile Picture
    95,548 Super User 2025 Season 2 on at
    Hi, hopefully the information below will give you some tips.
     
     
    Thanks
    ZHU
  • Shrey Chauhan Profile Picture
    284 on at
    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.
     

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,151

#2
Jainam M. Kothari Profile Picture

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

#3
YUN ZHU Profile Picture

YUN ZHU 1,092 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans