0

I'm facing an issue on sending an HTTP post request from Marketing cloud to ElasticSearch using SSJS. The aim is to send some import statistics of flat files to ElasticSearch in order to retrieve it into a monitoring dashboard.

Here is my script :

    <script runat="server">
    
    Platform.Load('core', '1');
    
    var log = DataExtension.Init("log_De_Customer_key");
    
    log.Rows.Add({"Date": Platform.Function.SystemDateToLocalDate(Now()), "Message": "Script starting..."});
    // LOG MESSAGE : Script starting...
    
    // Format date function to fit ElasticSearch format
    function formatDate(param) {
    
        // Format datepart to force 2 digits value for days and months
        function twoDigits(datePart) {
            
            var rawPart = datePart;
            if (datePart.length < 2) {
                return formPart = "0" + rawPart;
            } else {
                return formPart = rawPart;
            } 
    
        return formPart;
    
        };
    
        var date = new Date(param);
    
        // Get datepart: year
        var yyyy = date.getFullYear().toString();
    
        // Get and transform datepart: month
        var datePart = (date.getMonth() + 1).toString();
        var mm = twoDigits(datePart);
    
        // Get and transform datepart: day
        var datePart = date.getDate().toString();
        var dd = twoDigits(datePart);
    
        // Get and transform datepart: hours
        var datePart = date.getHours().toString();
        var hh = twoDigits(datePart);
    
        // Get and transform datepart: minutes
        var datePart = date.getMinutes().toString();
        var min = twoDigits(datePart);
    
        // Get and transform datepart: seconds
        var datePart = date.getSeconds().toString();
        var ss = twoDigits(datePart);
        
        // Assemble the final date string
        var finalDate = yyyy + '-' + mm + '-' + dd + 'T' + hh + ':' + min + ':' + ss + '.000000000Z';
    
        return finalDate;
    
        };
    
    // Get import DE information in order to retrieve the last import value
    var importDE = DataExtension.Init("statistics_De_Customer_Key");
    var data = importDE.Rows.Retrieve();
    var lastRow = data[data.length-1];
    
    // Set timestamp
    var timestamp = new Date().getTime().toString();
    
    // Set import date
    var param = new Date(lastRow.ImportDate);
    var importDate = formatDate(param);
    
    //Set filename
    var fileNameBase = 'FILE_PRODUIT_';
    var fileNameDate = importDate.replace(/[^a-zA-Z0-9]/g, '').substring(0, 8);
    var fileNameExt = '.csv';
    
    // Set import information
    var flowName = lastRow.Source;
    var fileName = fileNameBase + fileNameDate + fileNameExt;
    var importedRecords = lastRow.Imported;
    var injectedRecords = lastRow.Inserted;
    var rejectedRecords = lastRow.Rejected;
    
    // Set API call parameters
    var identify = DataExtension.Init("credentials_de_customer_key");
    var idData = identify.Rows.Retrieve();
    
    var url = idData[idData.length-1].url;
    var username = idData[idData.length-1].username;
    var password = idData[idData.length-1].password;
    var credentials = username + ':' + password;
    var encodedCredentials = Base64Encode(credentials);
    
    // Set API call header
    var contentType = 'application/json';
    var headerNames = ["Authorization"];
    var authorization = 'Basic ' + encodedCredentials;
    var headerValues = [authorization];
    
    // Set API call payload
    var payload = {
        "user" : username,
        "@timestamp" : timestamp,
        "SFMC_FlowName" : flowName,
        "SFMC_FileName" : fileName,
        "SFMC_ImportDate" : importDate,
        "SFMC_ImportedRecords" : importedRecords,
        "SFMC_InjectedRecords" : injectedRecords,
        "SFMC_RejectedRecords" : rejectedRecords
    };
    
    log.Rows.Add({"Date": Platform.Function.SystemDateToLocalDate(Now()), "Message": "payload: " + Stringify(payload)});
    // LOG MESSAGE : Payload
    
    // Perform API call and catch result
    try {
            
        var result = HTTP.Post(url, contentType, Stringify(payload), headerNames, headerValues);
        result = Stringify(result).replace(/[\n\r]/g, '');
        log.Rows.Add({"Date": Platform.Function.SystemDateToLocalDate(Now()), "Message": "result: " + result});
        // LOG MESSAGE : Result message
            
    } catch (e) {
        
        e = Stringify(e).replace(/[\n\r]/g, '');
        log.Rows.Add({"Date": Platform.Function.SystemDateToLocalDate(Now()), "Message": "error: " + e});
        // LOG MESSAGE : Error message
    }
    
    
    log.Rows.Add({"Date": Platform.Function.SystemDateToLocalDate(Now()), "Message": "Script ending..."});
    // LOG MESSAGE : Script finishing...
    
    </script>

And here is the error i'm getting in my logs :

error:

{
"message":"An error occurred when attempting to evaluate a HTTPPost function call.  See inner exception for details.",
"description":"ExactTarget.OMM.FunctionExecutionException: An error occurred when attempting to evaluate a HTTPPost function call.  See inner exception for details.\r\n  
Error Code: OMM_FUNC_EXEC_ERROR\r\n - from Jint --> \r\n\r\n 
--- inner exception 1---\r\n\r\n
System.Net.WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. - from System --> \r\n\r\n 
--- inner exception 2---\r\n\r\n
System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure. - from System\r\n\r\n\r\n\r\n\r\n\r\n"
}

I'm able to perform this request using postman with exactly the same parameter and the request is working well. There is no certificate issue, it has been signed Self-signed but I can't figure out why this could be an issue. Furthermore with no error code such as 400 or 503, i'm at a dead end. Any help would be appreciated, thank's in advance for your hand. Kind regards

4
  • Have you tried hardcoding the values to see if a static call works? – Noor Alam Nov 3 '20 at 12:56
  • I'm trying so, coming back to you after several trials. – Pierre Lhostis Nov 3 '20 at 13:23
  • Unfortunately, I have the same results with the raw values, and so tried to simplify the code as much as possible doing this on the post function : HTTP.Post('https://url:port/server/_doc/', 'application/json', payload, 'Authorization: ' + 'Basic ' + Platform.Function.Base64Encode("user:password")); I got this error in return error: {"message":"Unable to retrieve security descriptor for this frame.","description":"System.InvalidOperationException: Unable to retrieve security descriptor for this frame. - from mscorlib\r\n\r\n"} – Pierre Lhostis Nov 3 '20 at 15:11
  • Get the same error : { Error Code: OMM_FUNC_EXEC_ERROR\r\n - from Jint --> \r\n\r\n --- inner exception 1---\r\n\r\nSystem.Net.WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. - from System --> \r\n\r\n --- inner exception 2---\r\n\r\nSystem.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure. - from System\r\n\r\n\r\n\r\n\r\n\r\n"} – Pierre Lhostis Nov 3 '20 at 15:51
0

The issue was due to the auto-signed certificate. Salesforce need to get a certificate recognized by a partner to realize a post method.

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.