Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
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
Categories:

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans