0

I have this API call, for shortening a link through API, which I got to work in Postman:

enter image description here

enter image description here

enter image description here

I want to make the exact same call from an email in Marketing Cloud, and I'm using this script, which I have based on this documentation: https://developer.salesforce.com/docs/marketing/marketing-cloud/guide/ssjs_httpPost.html

    <script runat="server">
Platform.Load("Core", "1");


var url = 'https://api.short.io/links';
var contentType = 'application/json';
var payload = {
  "tags": ["SmsUnsubLink"],
  "domain": "mydomain.com",
  "originalURL": "https://www.mydomain.dk/test1",
  "allowDuplicates": true,
  "expiredURL": "https://www.google.com",
  "expiresAt": 1675594800000,
  "title": "myTitle"
};

var headerNames = ["Authorization", "accept"];
var headerValues = ["myAuthKey", "application/json"];
var result = HTTP.Post(url, contentType, payload, headerNames, headerValues);


Write(result.StatusCode + '<br>');
Write(result.Response);
  
  
  </script>

But I'm just getting this error: Unable to retrieve security descriptor for this frame. Line: 1803 Char: 8 responseDetails.StatusCode = Platform.Function.HTTPPost(url, contentType, payload, headerNames, headerValues, response)

I have tried changing a lot of minor things, but nothing really seems to work, I get the same error. What am I doing wrong? Please note that the postman example I've posted is working fine.

1 Answer 1

2

Personally I prefer using Script.Util.HttpRequest which is more robust (supports e.g. retries) and versatile - supports more request types:

  • GET
  • DELETE
  • HEAD
  • OPTIONS
  • PATCH
  • POST
  • PUT

Here is your code rewritten to use the above function:

<script runat="server">
Platform.Load("Core", "1");
try {
var payload = {
  "tags": ["SmsUnsubLink"],
  "domain": "mydomain.com",
  "originalURL": "https://www.example.com/test23",
  "allowDuplicates": true,
  "expiredURL": "https://www.google.com",
  "expiresAt": 1675594800000,
  "title": "myTitle"
};
  
  var linkShortener = new Script.Util.HttpRequest("https://api.short.io/links");  
  linkShortener.emptyContentHandling = 0;
  linkShortener.retries = 2;
  linkShortener.continueOnError = true;
  linkShortener.contentType = "application/json; charset=utf-8";
  linkShortener.setHeader("Authorization", "myAuthKey");
  linkShortener.setHeader("accept", "application/json");
  linkShortener.method = "POST"; 
  linkShortener.postData = Stringify(payload);
  
  var shortenLink = linkShortener.send();
  var linkShortenerResult = String(shortenLink.content);
  Write(linkShortenerResult);
} catch(error) {
Write(Stringify(error));
}
</script>

Using this approach provides me with a successful JSON response:

{
  "originalURL": "https://www.example.com/test23",
  "DomainId": 634149,
  "archived": false,
  "lcpath": "nmop5p",
  "title": "myTitle",
  "source": "api",
  "cloaking": false,
  "createdAt": "2023-02-22T08:21:32.806Z",
  "updatedAt": "2023-02-22T08:21:32.806Z",
  "OwnerId": 782976,
  "tags": [
    "SmsUnsubLink"
  ],
  "path": "nMOP5p",
  "idString": "lnk_2EYd_9dCPkogdBwj",
  "shortURL": "https://mydomain.com/nMOP5p",
  "secureShortURL": "https://mydomain.com/nMOP5p",
  "duplicate": false
}
3
  • I'm getting this error now: {"error":"Parameter domain is required","errorList":[{"msg":"Parameter domain is required","param":"domain","location":"body"},{"msg":"Please provide a valid originalURL parameter, it must be a valid url","param":"originalURL","location":"body"}]} So it appears to now accept the domain and originalURL, which is probably just because they are the only two required parameters. Commented Feb 22, 2023 at 8:08
  • 1
    @NikolaiHvid - This sounds more like an issue on the receiving end, but can you check with my updated code, where I made a small adjustments to how you are passing the payload? Commented Feb 22, 2023 at 8:15
  • Adding Stringify(payload); worked! Thanks! Commented Feb 22, 2023 at 8:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .