2

I'm trying to make API call to external system from a Cloud page, but when I publish I'm getting response Response:nullresultString. Is it something do with IP whitelisting or it's a code error. Below the code details.

Note: Original Idea is to test out the API via Cloud page and if everything works fine, I would like to use this code and put that in SMS Stop keyword response. I would like to know can I do that? if someone respond STOP I would like to update external system with optout status via API.

Eagerly looking for your valuable 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)) }  

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
            }
        ]
    }
}'

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy