RE: Business Central API integration with DHL API Service URL, API Key and API Secret
i have also integrated the calls to DHL. i am using user and password (the example ones are demo-key, demo-secret), i think they are the same for the ones you mean API-Key API-Secret. the code i use is this:
procedure DHL_HttpRequest()
begin
Token := AddHttpBasicAuthHeader('demo-key', 'demo-secret);
RequestMessage.SetRequestUri(URL);
Headers := Client.DefaultRequestHeaders();
Headers.Add('Authorization', Token);
Headers.Add('Message-Reference', LowerCase(DelChr(BlankGuid, '=', '{}')));
PostRequest(RequestMessage, 'application/json');
Client.Send(RequestMessage, ResponseMessage);
end;
local procedure AddHttpBasicAuthHeader(UserValue: Text; PasswordValue: Text): Text
var
Base64Convert: Codeunit "Base64 Convert";
AuthString: Text;
UserPassLbl: Label '%1:%2', Locked = true;
BasicLbl: Label 'Basic %1', Locked = true;
begin
AuthString := StrSubstNo(UserPassLbl, UserValue, PasswordValue);
AuthString := Base64Convert.ToBase64(AuthString);
AuthString := StrSubstNo(BasicLbl, AuthString);
exit(AuthString)
end;
local procedure PostRequest(var RequestMessageEdit: HttpRequestMessage; ContentType: Text)
var
RequestContent: HttpContent;
ContentHeaders: HttpHeaders;
begin
RequestContent.WriteFrom(Body);
RequestContent.GetHeaders(ContentHeaders);
ContentHeaders.Remove('Content-Type');
ContentHeaders.Add('Content-Type', ContentType);
RequestMessageEdit.Method('POST');
RequestMessageEdit.Content(RequestContent);
end;
Don't forget to help the community by Verifying the answer and Like it if your question has been answered. It will let others know that the topic has verified answer.
Thanks & Regards,
Gian