The CreatorCon Call for Content is officially open! Get started here.

post Rest message script in flow designer

chandan2212
Tera Contributor

Hi all , 

 

I am using a REST message with the POST method in Flow Designer. I am sending two input fields: phonenumber and workphone. I have created the REST message, set the endpoint, and configured basic authentication. Below is the preview script I’m using:

Can anyone confirm whether this script is correct or needs improvement?

 

(function execute(inputs, outputs) {
    var phonenumber = inputs.phonenumber;
    var workphone = inputs.workphone;

    try {
        var r = new sn_ws.RESTMessageV2('rest message''post message');

        // If you're using parameters in the REST message body template
        r.setStringParameterNoEscape('phonenumber', phonenumber);
        r.setStringParameterNoEscape('workphone', workphone);

        // Alternatively, if you're building the body manually:
        // var requestBody = JSON.stringify({
        //     "phonenumber": phonenumber,
        //     "workphone": workphone
        // });
        // r.setRequestBody(requestBody);

        var response = r.execute();
        var responseBody = response.getBody();
        var httpStatus = response.getStatusCode();

        // Optionally log or output the response
        outputs.responseBody = responseBody;
        outputs.httpStatus = httpStatus;
        output.responseBody=responseBody;
    } catch (ex) {
        var message = ex.message;
        outputs.error = message;
    }
})(inputs, outputs);
 
 
whether this script is correct or wrong ?
2 REPLIES 2

anurampalli
Tera Contributor

It looks good to go.

 

If you copied the code sample from your post method of the rest outbound message, you will get correct values for 'rest message' and 'post message' in the line below.

 

var r = new sn_ws.RESTMessageV2('rest message''post message');

Ct111
Tera Sage

try something like this 

 

 

(function execute(inputs, outputs) {
var phonenumber = inputs.phonenumber;
var workphone = inputs.workphone;

try {
var r = new sn_ws.RESTMessageV2('rest message', 'post message');

// Option 1: Using parameters if placeholders exist in REST message body
r.setStringParameterNoEscape('phonenumber', phonenumber);
r.setStringParameterNoEscape('workphone', workphone);

// Option 2: OR build request body directly (if no placeholders)
// var requestBody = JSON.stringify({
// "phonenumber": phonenumber,
// "workphone": workphone
// });
// r.setRequestBody(requestBody);

var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

outputs.responseBody = responseBody;
outputs.httpStatus = httpStatus;

if (httpStatus !== 200) {
outputs.error = "Unexpected HTTP status: " + httpStatus;
}
} catch (ex) {
outputs.error = ex.message;
}
})(inputs, outputs);