Skip to main content

Notifications

Announcements

No record found.

Business Central forum
Unanswered

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

Posted on by 288
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;

Another Code :


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;
 
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

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 16th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

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

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 228,177 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans