I'm trying to make API call to external system from a Cloud page, but when I publish I'm getting response Response:nullresultString:. Below the code details. Can someone please help me this. Thank you and looking forward to hear your inputs.
Code that put in Cloud page to make API call
<script runat=server>
Platform.Load("core", "1.1.1");
try{
var accessToken = 'XXXXXXXX';
var clientid = 'KKKKKKKK';
var clientsecret = 'YYYYYYYYY';
var url = 'https://endpointurl.com'
var payload = '{"requestHeader": {"consumer": {"name": "SFMC","id": "SFMC","businessUnit": "CUSTOMER PROFILING","type": "EXTERNAL","clientVersion": "V1","requestDateTime": "2018-07-28 22:27:56:048","hostName": "uapp9077n","businessTransactionType": "ProfileAndPreferenceWrapper","contextId": "","secondContextId": "","thirdContextId": ""},"credentials": {"userName": "","password": "","token": "aip_personalization","type": "jwt"},"transactionId": "31a1475d-0a64-467e-9933-5c928172d145"},"requestBody": {"preferences" :[{"phoneNumber": "565655656","isOptedIn": "false", "isImplicit": "true"}]}}';
var auth = 'Basic ' + accessToken;
var req = new Script.Util.HttpRequest(url);
req.emptyContentHandling = 0;
req.retries = 2;
req.continueOnError = true;
req.contentType = "application/json"
req.setHeader("Authorization", auth);
req.setHeader("x-ibm-client-id", clientid);
req.setHeader("x-ibm-client-secret", clientsecret);
req.method = "POST"; /*** You can change the method here ***/
req.postData = payload;
var resp = req.send();
var resultString = String(resp.content)
var resultJSON = Platform.Function.ParseJSON(String(resp.content));
Write("Response:"+resultJSON);
Write("resultString:"+resultString);
} catch (e) { Write(Stringify(e)) }
</script>
Here the curl that works in postman
curl --location --request POST 'https://endpointurl.com' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic XXXXXXXX' \
--header 'x-ibm-client-id: KKKKKKKK' \
--header 'x-ibm-client-secret: YYYYYYYYY' \
--data-raw '{
"requestHeader": {
"consumer": {
"name": "SFMC",
"id": "SFMC",
"businessUnit": "CUSTOMER PROFILING",
"type": "EXTERNAL",
"clientVersion": "V1",
"requestDateTime": "2018-07-28 22:27:56:048",
"hostName": "uapp9077n",
"businessTransactionType": "ProfileAndPreferenceWrapper",
"contextId": "",
"secondContextId": "",
"thirdContextId": ""
},
"transactionId": "31a1475d-0a64-467e-9933-5c928172d145"
},
"requestBody": {
"preferences": [
{
"phoneNumber": "565655656",
"isOptedIn": false,
"isImplicit": true
}
]
}
}'
try { ...your code... } catch(e) { Write(Stringify(e)) }
- need Core library called to use example as is) and see if you can get more details on the error. The most likely culprit I can see would be the payload as if there is a'
or something else that can malform the JSON string in it, it will blow up. I would recommend building it out as a JSON and then Stringifying it to push to payload.