Friday, 11 September 2020

ServiceNow send POST REQUEST EXAMPLE

 https://community.servicenow.com/community?id=community_question&sys_id=7f3df786db7a3700414eeeb5ca961928


We can use JSON and few related methods as below.

REST Message POST Method.

 

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.


No comments:

Post a Comment