I am trying to read Content from "Text" type Content Builder Asset. The Documentation states that the Content of the Asset is Embedded along with Html Code. The Response from Rest API should be able to escape the Html Tags right?
Here is the code sample.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1> Fetching Content Builder Templates </h1>
<script type = "javascript" runat = "server" >
Platform.Load("Core", "1.1.5");
var host = "****";
var account_id = "****";
var grant_type = "client_credentials";
var client_id = "****";
var client_secret = "****";
// Get API AccessToken
var token_url = 'https://' + host + '.auth.marketingcloudapis.com/v2/token';
var contentType = 'application/json';
var token_payload = '{"grant_type":"' + grant_type + '",';
token_payload += '"client_id":"' + client_id + '",';
token_payload += '"client_secret":"' + client_secret + '",';
token_payload += '"account_id":"' + account_id + '"}';
var accessToken = "", assets_response = "", assets_response_escaped= "" ;
try {
// getting access token
var accessTokenResult = HTTP.Post(token_url, contentType, token_payload);
var statusCode = accessTokenResult["StatusCode"];
var response = accessTokenResult["Response"][0];
accessToken = Platform.Function.ParseJSON(response).access_token;
Write('accessToken->:' + accessToken);
if (accessToken != "") {
//getting content builder templates from assets
try {
var assets_url = 'https://' + host + '.rest.marketingcloudapis.com/asset/v1/content/assets/query';
var assets_payload = Stringify({
"query": {
"leftOperand": {
"property": "name",
"simpleOperator": "LIKE",
"value": "PROMOTIONAL"
},
"logicalOperator": "OR",
"rightOperand": {
"property": "name",
"simpleOperator": "LIKE",
"value": "TRANSACTIONAL"
}
},
"sort": [{
"property": "id",
"direction": "DESC"
}],
"fields": [
"category",
"content",
"data"
]
});
var assets_auth = "Bearer " + accessToken;
//making API call to get Assets from Content Builder
var assetsPOSTResult = HTTP.Post(assets_url, contentType, assets_payload, ['Authorization'], [assets_auth]);
//Write("<br>assetsPOSTResult::"+Stringify(assetsPOSTResult));
var assets_statusCode = assetsPOSTResult["StatusCode"];
if (assets_statusCode == 200) {
assets_response = assetsPOSTResult["Response"][0];
Write("<br><br>assets_response::" + assets_response);
assets_response_escaped = assets_response.replace(/<\/?[^>]+>/gi, '');//to remove html tags
Write("<br><br>assets_response_escaped without html::" + assets_response_escaped);
}
} catch (err) {
Write("<br>assets API error::" + Stringify(err));
}
} //end of AccessToken if loop
} catch (err) {
Write("<br>Token API error::" + Stringify(err));
}
</script>
<script runat="client">
var finalassets_response='<ctrl:var name="assets_response" />';
console.log('clientside finalassets_response::'+finalassets_response);
var finalassets_response2='<ctrl:var name="assets_response_escaped" />';
console.log('clientside finalassets_response2::'+finalassets_response2);
var assetsRespJSON = JSON.parse(finalassets_response2);
var itemsArray=assetsRespJSON.items;
console.log('itemsArray::'+itemsArray);
itemsArray.forEach(function(item) {
var customerKey = item.customerKey;
console.log('customerKey->', customerKey);
});
</script>
</body>
</html>
This is the response I got from console logs.
clientside finalassets_response::{"count":6,"page":1,"pageSize":50,"links":{},"items":[{"id":31762,"customerKey":"Transactional_Template3","assetType":{"id":196,"name":"textblock","displayName":"Text Block"},"name":"Transactional_Template3","category":{"id":34635,"name":"TRANSACTIONAL_TEMPLATES","parentId":34605},"content":"<table cellpadding="0" cellspacing="0" width="100%" role="presentation" style="min-width: 100%; " class="stylingblock-content-wrapper"><tr><td class="stylingblock-content-wrapper camarker-inner">Hi<br>
Your Transaction failed</td></tr></table>","modelVersion":2},{"id":31761,"customerKey":"Transactional_Template2","assetType":{"id":196,"name":"textblock","displayName":"Text Block"},"name":"Transactional_Template2","category":{"id":34635,"name":"TRANSACTIONAL_TEMPLATES","parentId":34605},"content":"<table cellpadding="0" cellspacing="0" width="100%" role="presentation" style="min-width: 100%; " class="stylingblock-content-wrapper"><tr><td class="stylingblock-content-wrapper camarker-inner">Hi<br>
Your Transaction was Successful</td></tr></table>","modelVersion":2},{"id":31760,"customerKey":"Transactional_Template1","assetType":{"id":196,"name":"textblock","displayName":"Text Block"},"name":"Transactional_Template1","category":{"id":34635,"name":"TRANSACTIONAL_TEMPLATES","parentId":34605},"content":"<table cellpadding="0" cellspacing="0" width="100%" role="presentation" style="min-width: 100%; " class="stylingblock-content-wrapper"><tr><td class="stylingblock-content-wrapper camarker-inner">Hi<br>
Please complete your Transaction.<br>
Cheers</td></tr></table>","modelVersion":2},{"id":31759,"customerKey":"Promotional_Template3","assetType":{"id":196,"name":"textblock","displayName":"Text Block"},"name":"Promotional_Template3","category":{"id":34634,"name":"PROMOTIONAL_TEMPLATES","parentId":34605},"content":"<table cellpadding="0" cellspacing="0" width="100%" role="presentation" style="min-width: 100%; " class="stylingblock-content-wrapper"><tr><td class="stylingblock-content-wrapper camarker-inner">Hi<br>
Please find your Weekend Coupon %%coupon%%</td></tr></table>","modelVersion":2},{"id":31758,"customerKey":"Promotional_Template2","assetType":{"id":196,"name":"textblock","displayName":"Text Block"},"name":"Promotional_Template2","category":{"id":34634,"name":"PROMOTIONAL_TEMPLATES","parentId":34605},"content":"<table cellpadding="0" cellspacing="0" width="100%" role="presentation" style="min-width: 100%; " class="stylingblock-content-wrapper"><tr><td class="stylingblock-content-wrapper camarker-inner">Hi<br>
Please find your Weekend Coupon %%coupon%%</td></tr></table>","modelVersion":2},{"id":31670,"customerKey":"Promotional_Template1","assetType":{"id":196,"name":"textblock","displayName":"Text Block"},"name":"Promotional_Template1","category":{"id":34634,"name":"PROMOTIONAL_TEMPLATES","parentId":34605},"content":"<table cellpadding="0" cellspacing="0" width="100%" role="presentation" style="min-width: 100%; " class="stylingblock-content-wrapper"><tr><td class="stylingblock-content-wrapper camarker-inner">Hi<br>
Please find your Voucher Code</td></tr></table>","modelVersion":2}]}
VM22 M1_UAT_SMS_Dynamic:15
clientside finalassets_response2::{"count":6,"page":1,"pageSize":50,"links":{},"items":[{"id":31762,"customerKey":"Transactional_Template3","assetType":{"id":196,"name":"textblock","displayName":"Text Block"},"name":"Transactional_Template3","category":{"id":34635,"name":"TRANSACTIONAL_TEMPLATES","parentId":34605},"content":"Hi
Your Transaction failed","modelVersion":2},{"id":31761,"customerKey":"Transactional_Template2","assetType":{"id":196,"name":"textblock","displayName":"Text Block"},"name":"Transactional_Template2","category":{"id":34635,"name":"TRANSACTIONAL_TEMPLATES","parentId":34605},"content":"Hi
Your Transaction was Successful","modelVersion":2},{"id":31760,"customerKey":"Transactional_Template1","assetType":{"id":196,"name":"textblock","displayName":"Text Block"},"name":"Transactional_Template1","category":{"id":34635,"name":"TRANSACTIONAL_TEMPLATES","parentId":34605},"content":"Hi
Please complete your Transaction.
Cheers","modelVersion":2},{"id":31759,"customerKey":"Promotional_Template3","assetType":{"id":196,"name":"textblock","displayName":"Text Block"},"name":"Promotional_Template3","category":{"id":34634,"name":"PROMOTIONAL_TEMPLATES","parentId":34605},"content":"Hi
Please find your Weekend Coupon %%coupon%%","modelVersion":2},{"id":31758,"customerKey":"Promotional_Template2","assetType":{"id":196,"name":"textblock","displayName":"Text Block"},"name":"Promotional_Template2","category":{"id":34634,"name":"PROMOTIONAL_TEMPLATES","parentId":34605},"content":"Hi
Please find your Weekend Coupon %%coupon%%","modelVersion":2},{"id":31670,"customerKey":"Promotional_Template1","assetType":{"id":196,"name":"textblock","displayName":"Text Block"},"name":"Promotional_Template1","category":{"id":34634,"name":"PROMOTIONAL_TEMPLATES","parentId":34605},"content":"Hi
Please find your Voucher Code","modelVersion":2}]}
VM23:1 Uncaught SyntaxError: Unexpected token
in JSON at position 295
at JSON.parse (<anonymous>)
at VM22 M1_UAT_SMS_Dynamic:16
The Same API when hit via Postman, I could see the escaped characters for Html Tags. What am i doing wrong??
Write()
to the page, you should be able to see the results. I would bet that it is correctly escaped - just console log is removing them for its display.clientside finalassets_response::"{\"count\":6,\"page\":1,\"pageSize\":50,\"links\":{},\"items\":[{\"id\":31762,\"customerKey\":\"Transactional_Template3\",\"assetType\":{\"id\":196,\"displayName\":\"Text Block\"},\"name\":\"Transactional_Template3\",\"category\":{\"id\":34634,\"name\":\"PROMOTIONAL_TEMPLATES\",\"parentId\":34605},\"content\":\"<table cellpadding=\"0\" cellspacing=\"0\" class=\"stylingblock-content-wrapper\"><tr><td class=\"camarker-inner\">Hi<br>\nPlease find your Voucher Code</td></tr></table>\",\"modelVersion\":2}]}"
<
and>
with>
and<
correspondingly and it will display it via Write as its no longer recognizing the tags as tags.assets_response = Stringify(assetsPOSTResult["Response"][0]); assets_response= assets_response.replace('<','<').replace( '>','>') ;
No Luck yet. The Response itself is coming under double quotes as its stringifed.