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;
Hi after adding the above details, i am getting misused header name error while sending the details, any idea about this.
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.
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?
Thanks to Fernando Tornero (@ github), I found the same procedure in a new codeunit “Base64 Helpers” (codeunit 4110)
Thanks Sven, much appreciated!
And thanks to you, you helped me
But now it looks like it is named Base64 Convert
And it would be typeHelper.ToBase64
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.
Thanks Steven, updates very much appreciated!
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=
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;
“`