0

it seems I'm stuck with the SSJS code to retrieve the short.io response.Do you know why i'm getting the no response or status -2 call failed as per HTTP.Response?

Please find below my code and screenshots showing the POSTMan results.

<script runat=server>
  
Platform.Load("core", "1.1.1");

    var payload = '{';
    payload += '"domain": "i58.short.gy",'
    payload += '"https://mail.google.com/mail/u/0/?tab=rm&ogbl#inbox/FMfcgxwLtZxtzfxMzzdMfqMbsgSbdZTq",'
    payload += '"allowDuplicates": false'
    payload += '}';
  
   var auth = 'XXX';
 
    var req = new Script.Util.HttpRequest('https://api.short.io/api/links');
    req.emptyContentHandling = 0;
    req.retries = 2;
    req.continueOnError = true;
    req.contentType = ("Content-Type","application/json");
    req.setHeader("authorization", auth);
    req.method = "POST";
    req.postData = payload;

    var resp = req.send();
 
    try {
        var result_status = Platform.Function.ParseJSON(String(resp.returnStatus));
        var result_status_code = Platform.Function.ParseJSON(String(resp.statusCode));
        var result_header = Platform.Function.ParseJSON(String(resp.headers));
        var result_content = Platform.Function.ParseJSON(String(resp.contentType));
 
        Platform.Response.Write("<br>Status:"+result_status +"");
        Platform.Response.Write("<br>Status Code:"+result_status_code +"");
        Platform.Response.Write("<br>Return header: "+result_header+"");
        Platform.Response.Write("<br>Return content: "+result_content+"");
      
    } catch (e) {
        Write(Stringify(e));
    }
</script>

These are the CloudPage results and PostMAN evidence that I get a 200 success status response. enter image description here

enter image description here

enter image description here

If you encounter any solution, I will be gladly appreciated ! Many thanks

1

In your payload in SSJS section, you have forgotten to include the "originalURL": property and instead just put in the URL:

    var payload = '{';
    payload += '"domain": "i58.short.gy",'
    payload += '"https://mail.google.com/mail/u/0/?tab=rm&ogbl#inbox/FMfcgxwLtZxtzfxMzzdMfqMbsgSbdZTq",'
    payload += '"allowDuplicates": false'
    payload += '}';

You would need to do:

    var payload = '{';
    payload += '"domain": "i58.short.gy",'
    payload += '"originalURL": "https://mail.google.com/mail/u/0/?tab=rm&ogbl#inbox/FMfcgxwLtZxtzfxMzzdMfqMbsgSbdZTq",'
    payload += '"allowDuplicates": false'
    payload += '}';

Or to make things easier for editing and maintaining, etc, you could do it like this:

var payload = {}
payload.domain = 'i58.short.gy';
payload.originalURL = 'https://mail.google.com/mail/u/0/?tab=rm&ogbl#inbox/FMfcgxwLtZxtzfxMzzdMfqMbsgSbdZTq'
payload.allowDuplicates = false
var payload = Stringify(payload);

This will assign directly the properties and easily differentiate the values in the object. It then Stringifies it at the end to make it digestible to the API call.

Other option is you could just write out the Object completely then Stringify, like:

var payload = {
    "domain": "i58.short.gy",
    "originalURL": "https://mail.google.com/mail/u/0/? tab=rm&ogbl#inbox/FMfcgxwLtZxtzfxMzzdMfqMbsgSbdZTq",
    "allowDuplicates": false
}
var payload = Stringify(payload);

Which then lets manipulate the object directly and removes the issues around building it as a string.

This

1
  • 1
    Many thanks for the additional suggestions to build the payload content - I overlooked the parameter and now I receive the correct response. – Ivan S. Apr 30 at 9:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.