Hello Friends,
Below is sample code to create multiple level Json Body Objects and send it on HTTP Request
Json Sample Body - need to generate:
{
"type": "media-notification",
"mime": "application/pdf",
"link": http://454.356.45.467:7146/Attachment/OrderEntry_19_01_2022_17_00_01_2955.pdf,
"body": {
"to": "919758642372",
"ttl": 200000,
"type": "template",
"template": {
"namespace": "11382ca3_9ff1_467f_acab_c722c0920145",
"language": {
"policy": "deterministic",
"code": "en"
},
"name": "sales_invoice_update",
"components": [
{
"type": "header",
"parameters": [
{
"type": "document",
"document": {
"filename": "Business-portal-connector-EULA.pdf"
}
}
]
},
{
"type": "body",
"parameters": [
{
"type": "text",
"text": "test"
},{
"type": "text",
"text": "{{text2}}"
},{
"type": "text",
"text": "{{text3}}"
},{
"type": "text",
"text": "{{text4}}"
},{
"type": "text",
"text": "{{text5}}"
},{
"type": "text",
"text": "{{text6}}"
}
]
}
]
}
}
}
================
Business Central AL Extension Code to Create Above body and send on HTTP Request
codeunit 76676 APIMgmt
{
PROCEDURE TestWhatsapp(): Boolean
VAR
Win_lDlg: Dialog;
FinalJobject: JsonObject;
bodyJobject: JsonObject;
templateJobject: JsonObject;
languageJobject: JsonObject;
componentsJArray: JsonArray;
componentsHeaderJObject: JsonObject;
componentsBodyJObject: JsonObject;
parametersJArray: JsonArray;
parametersJObject: JsonObject;
DocumentJObject: JsonObject;
parametersBodyJArray: JsonArray;
parametersBodyJObject: JsonObject;
ltext: Text;
lheaders: HttpHeaders;
lurl: Text;
gcontent: HttpContent;
gHttpClient: HttpClient;
gheaders: HttpHeaders;
greqMsg: HttpRequestMessage;
gResponseMsg: HttpResponseMessage;
JSONResponse: Text;
JObject: JsonObject;
Jtoken: JsonToken;
JValue: JsonValue;
BEGIN
IF NOT CONFIRM('Do you want to Create API?', FALSE) THEN
EXIT;
Win_lDlg.OPEN('Processing...');
Clear(FinalJobject);
Clear(bodyJobject);
Clear(templateJobject);
Clear(languageJobject);
Clear(componentsJArray);
Clear(componentsHeaderJObject);
Clear(componentsBodyJObject);
Clear(parametersJArray);
Clear(parametersJObject);
Clear(DocumentJObject);
Clear(parametersBodyJArray);
Clear(parametersBodyJObject);
//Main Body Objects
FinalJobject.Add('type', 'media-notification');
FinalJobject.Add('mime', 'application/pdf');
FinalJobject.Add('link', 'http://XXXX.....1960_01_2955.pdf');
//Body Objects
bodyJobject.Add('to', '919426481901');
bodyJobject.Add('ttl', 200000);
bodyJobject.Add('type', 'template');
templateJobject.Add('namespace', '11382ca3_9ff1_445f_acab_c722c0920173');
languageJobject.Add('policy', 'deterministic');
languageJobject.Add('code', 'en');
templateJobject.Add('language', languageJobject);
templateJobject.Add('name', 'sales_invoice_update');
//Header Component
componentsHeaderJObject.Add('type', 'header');
parametersJObject.Add('type', 'document');
DocumentJObject.Add('filename', 'Business-portal-connector-EULA.pdf');
parametersJObject.add('document', DocumentJObject);
parametersJArray.Add(parametersJObject);
componentsHeaderJObject.Add('parameters', parametersJArray);
componentsBodyJObject.Add('type', 'body');
Clear(parametersBodyJObject);
parametersBodyJObject.Add('type', 'text');
parametersBodyJObject.Add('text', 'test');
parametersBodyJArray.Add(parametersBodyJObject);
Clear(parametersBodyJObject);
parametersBodyJObject.Add('type', 'text');
parametersBodyJObject.Add('text', '{{test2}}');
parametersBodyJArray.Add(parametersBodyJObject);
Clear(parametersBodyJObject);
parametersBodyJObject.Add('type', 'text');
parametersBodyJObject.Add('text', '{{test3}}');
parametersBodyJArray.Add(parametersBodyJObject);
Clear(parametersBodyJObject);
parametersBodyJObject.Add('type', 'text');
parametersBodyJObject.Add('text', '{{test4}}');
parametersBodyJArray.Add(parametersBodyJObject);
Clear(parametersBodyJObject);
parametersBodyJObject.Add('type', 'text');
parametersBodyJObject.Add('text', '{{test5}}');
parametersBodyJArray.Add(parametersBodyJObject);
Clear(parametersBodyJObject);
parametersBodyJObject.Add('type', 'text');
parametersBodyJObject.Add('text', '{{test6}}');
parametersBodyJArray.Add(parametersBodyJObject);
componentsBodyJObject.Add('parameters', parametersBodyJArray);
componentsJArray.Add(componentsHeaderJObject);
componentsJArray.Add(componentsBodyJObject);
templateJobject.Add('components', componentsJArray);
bodyJobject.Add('template', templateJobject);
FinalJobject.Add('body', bodyJobject);
FinalJobject.WriteTo(ltext);
MESSAGE(FORMAT(ltext));
lurl := 'https://app.yellowmessenger.com/integr80225396';
gcontent.WriteFrom(ltext);
greqMsg.SetRequestUri(lurl);
lheaders.Clear();
gcontent.GetHeaders(lheaders);
lheaders.Add('x-auth-token', '231910d304bcad66eeb83b3770');
lheaders.Remove('Content-Type');
lheaders.Add('Content-Type', 'application/json');
gcontent.GetHeaders(lheaders);
greqMsg.Content(gcontent);
greqMsg.GetHeaders(lheaders);
greqMsg.Method := 'POST';
if not gHttpClient.Send(greqMsg, gResponseMsg) then
Error('API Authorization token request failed...');
JSONResponse := '';
gResponseMsg.Content().ReadAs(JSONResponse);
Message(JSONResponse);
if not JObject.ReadFrom(JSONResponse) then
Error('Invalid response, expected a JSON object');
Win_lDlg.CLOSE;
EXIT(TRUE);
END;
}
That's it
It is very simple to create Json Object and send it on using HTTP Request
Thank you for reading...
Keep Sharing....Keep Growing....
No comments:
Post a Comment