HTTP Basic Authentication with the AL HttpClient

Business Central and the AL language have made web service code much easier with the HttpClient and Json types available. Handling the HTTP Authorization header is easier too with the TempBlob table, which can now encode the basic authentication string using base64.

See below for an example of how to add a basic authorisation header to the AL HttpClient:

procedure AddHttpBasicAuthHeader(UserName: Text[50]; Password: Text[50], var HttpClient : HttpClient);
var
  AuthString: Text;
  TempBlob: Record TempBlob temporary;
begin
  AuthString := STRSUBSTNO('%1:%2, UserName, Password);
  TempBlob.WriteTextLine(AuthString);
  AuthString := TempBlob.ToBase64String();
  AuthString := STRSUBSTNO('Basic %1', AuthString);
  HttpClient.DefaultRequestHeaders().Add('Authorization', AuthString);
end;

Update 2019-07-04: Thanks to Arend-Jan Kauffmann commenting on LinkedIn to point out there is an even easier way to get the Base64 encoding done using Codeunit 10 “Type Helper”:

procedure AddHttpBasicAuthHeader(UserName: Text[50]; Password: Text[50], var HttpClient : HttpClient);
var
  AuthString: Text;
  TypeHelper: "Type Helper";
begin
  AuthString := STRSUBSTNO('%1:%2, UserName, Password);
  AuthString := TypeHelper.ConvertValueToBase64(AuthString);
  AuthString := STRSUBSTNO('Basic %1', AuthString);
  HttpClient.DefaultRequestHeaders().Add('Authorization', AuthString);
end;

10 thoughts on “HTTP Basic Authentication with the AL HttpClient”

  1. Hi after adding the above details, i am getting misused header name error while sending the details, any idea about this.

  2. Hi Varun, are you able to share a code snippet showing how you create the auth string and add the header? Also which version of NAV/BC are you working on? I recall a similar error when using the .NET libraries in NAV as it doesn’t like you directly adding an Authorisation header to the HTTPRequest, and requires the use of a specific function to add credentials.

  3. In BC ocktober 2019 release (version 15) its looks like the .ConvertValueToBase64 is removed from the codeunit 10 (Type Helper). Do you know if this procedure has been just moved or completly removed?

  4. Thanks, this is very helpful.

    I submit an update for V17 of BC :
    procedure AddHttpBasicAuthHeader(UserName: Text[50]; Password: Text[50]; var HttpClient: HttpClient);
    var
    AuthString: Text;
    Base64Helpers: Codeunit “Base64 Convert”;
    begin
    AuthString := STRSUBSTNO(‘%1:%2’, UserName, Password);
    AuthString := Base64Helpers.ToBase64(AuthString);
    AuthString := STRSUBSTNO(‘Basic %1’, AuthString);
    HttpClient.DefaultRequestHeaders().Add(‘Authorization’, AuthString);
    end;

    Hope this helps.

    1. Hi Steven/Dan – I am trying to generate a base64 string from the codeunit and passing it in my authstring. If I paste the AuthString generated by postman API, the API response is succesful. Below are both the AuthStrings. Any suggestions/ideas?

      The first one is via POSTMAN, second one via base64 codeunit in AL. I am working on the latest version of BC SaaS

      Basic dGVzdF9kcEIxSDV4Y3V0eWNkam1VZVk0REJDSE4wQm96YlN6bExPOg==

      Basic dGVzdF9kcEIxSDV4Y3V0eWNkam1VZVk0REJDSE4wQm96YlN6bExPICA=

  5. Thanks for the hints Dan.

    Reworked in 2022
    “`
    [NonDebuggable]
    local procedure AddBasicAuthHeader(HMGSetup: Record vfs_dp1_HMGSetup; var ReqHeaders: HttpHeaders)
    var
    Base64: Codeunit “Base64 Convert”;
    Password: Text;
    AuthString: Text;
    AuthStringTxt: Label ‘%1:%2’, Locked = true;
    AuthHeaderTxt: Label ‘Basic %1’, Locked = true;
    AuthHeaderLbl: Label ‘Authorization’, Locked = true;
    begin
    IsolatedStorage.Get(HMGSetup.”Password GUID”, DataScope::Module, Password);
    AuthString := STRSUBSTNO(AuthStringTxt, HMGSetup.”User ID”, Password);
    AuthString := Base64.ToBase64(AuthString);
    AuthString := STRSUBSTNO(AuthHeaderTxt, AuthString);
    ReqHeaders.Add(AuthHeaderLbl, AuthString);
    end;

    “`

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.