We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

POST method in outbound REST message

Rj27
Mega Guru

Hi All,

Can we define HTTP query parameters of post method (defined in outbound REST message) using any script?

find_real_file.png

 

My doubt is- here in script I am setting the parameters but there is a possibility there might be no value passed for any one of them (suppose field 'memory').

In that case when no value is there for memory, entire field/key named 'memory' should not be there in the content Shared above. The case SHOULD NOT be like "memory":' '. Rather there should be no memory at all.

find_real_file.png

 

TIA,

-Ruchi

6 REPLIES 6

Vishal Khandve
Kilo Sage

Hi Ruchi,

 have you added the variable substitution as below

 

find_real_file.png

 

Thanks,

Vishal

Baala T
Mega Guru

Hi Ruchi,

We can use JSON and few related methods as below.

REST Message POST Method.

find_real_file.png

 

and the script should be as below

var sName = 'Bala_0007';
var sAge = '30';
var sSalary = '';

//Build JSON Object
var reqBody = {};

if (sName)
    reqBody['name'] = sName;

if (sSalary)
    reqBody['salary'] = sSalary;

if (sAge)
    reqBody['age'] = sAge;

try {
	//JSON to String
	var sReqBodyData = JSON.stringify(reqBody);
	
    var r = new sn_ws.RESTMessageV2('Dummy REST API', 'POST');
    r.setStringParameterNoEscape('messageBody', sReqBodyData);

    var response = r.execute();

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

    gs.print(JSON.parse(responseBody));
} catch (ex) {
    var message = ex.message;
    gs.print(message);
}

This code will adds the field name which has value and send the same.

 

Regards,
Bala T