data-urlencode in C/AL NAV importiern [Gelöst]

10. Juli 2024 15:49

Hallo Zusammen,

Wie kann ich den Curl-Code in NAV C/AL umwandeln? Kann mir jemand helfen? nur mit AddBody


curl --location 'https://api-sandbox.dhl.com/parcel/de/account/auth/ropc/v1/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'client_id=XXXXXXX' \
--data-urlencode 'username=user-valid' \
--data-urlencode 'password=SandboxPasswort2023!' \
--data-urlencode 'client_secret=XXXXXXXXX'


C/AL Code
URL := 'https://api-sandbox.dhl.com/parcel/de/account/auth/ropc/v1/token';
HttpWebRequestMgt.Initialize(URL);
HttpWebRequestMgt.SetMethod('POST');
HttpWebRequestMgt.SetReturnType('APPLICATION/JSON');
HttpWebRequestMgt.SetContentType('application/x-www-form-urlencoded');

granttype := 'grant_type';
clientid := 'client_id';
clientisec := 'client_secret';
username := 'username';
password := 'password';
granttype_Value := 'password';
clientid_Value := 'XXXXXXX';
clientisec_Value := 'XXXXXXXXX';
username_Value := 'XXXXXXXX';
password_Value := 'XXXXXXXXXX';

// In diesem Fall erscheint in der zweiten Zeile eine Fehlermeldung
// HttpWebRequestMgt.AddBodyAsText(TypeHelper.UrlEncode(granttype) + '=' + TypeHelper.UrlEncode(granttype_V));
// HttpWebRequestMgt.AddBodyAsText(TypeHelper.UrlEncode(clientid) + '=' + TypeHelper.UrlEncode(clientid_V));
// HttpWebRequestMgt.AddBodyAsText(TypeHelper.UrlEncode(username) + '=' + TypeHelper.UrlEncode(username_V));
// HttpWebRequestMgt.AddBodyAsText(TypeHelper.UrlEncode(password) + '=' + TypeHelper.UrlEncode(password_V));
// HttpWebRequestMgt.AddBodyAsText(TypeHelper.UrlEncode(clientisec) + '=' + TypeHelper.UrlEncode(clientisec_V));

RequestBody := TypeHelper.UrlEncode(granttype) + '=' + TypeHelper.UrlEncode(granttype_Value) + '&' +
TypeHelper.UrlEncode(clientid) + '=' + TypeHelper.UrlEncode(clientid_Value) + '&' +
TypeHelper.UrlEncode(username) + '=' + TypeHelper.UrlEncode(username_Value) + '&' +
TypeHelper.UrlEncode(password) + '=' + TypeHelper.UrlEncode(password_Value) + '&' +
TypeHelper.UrlEncode(clientisec) + '=' + TypeHelper.UrlEncode(clientisec_Value);

HttpWebRequestMgt.AddBodyAsText(RequestBody);
HttpWebRequestMgt.SendRequestAndReadResponse(TempBlob, ErrorText, ErrorDetailsText, HttpStatusCode, ResponseHeaders);
CLEAR(TextBody);
CLEAR(JSONBufferHeader);
JSONBufferHeader.ReadFromBlob(TempBlob);
JSONBufferHeader.SETRANGE("Token type", JSONBufferHeader."Token type"::String);
IF JSONBufferHeader.FINDSET() THEN BEGIN
REPEAT
IF TextBody <> '' THEN
TextBody += CRFL;
TextBody += STRSUBSTNO('%1 = %2',JSONBufferHeader.Path,JSONBufferHeader.Value);
UNTIL JSONBufferHeader.NEXT() = 0;
END;
MESSAGE(TextBody);
//-------------------------------------------------------------