Need help with REST api with json format

Jerry121
Kilo Contributor

I have a requirement that is

Need to send variables that are submitted by user on a form using REST API. I created and tested the api it is working fine. But, I am trying to send the variables through workflow by calling rest message. But, when I send them it is giving "unable to parse the request details" then I got to know that I am not sending the variables in the required format. I don't know how to send variables in this format.

Can you please help me out how to send them using BR. I am using run script in workflow now some thing like

r.setStringParameterNoEscape('requestName',current.variables.Username);

Here is the format

{

  "var1": "test1",

  "var2": "Test2",

  "var3": "test3",

  "var4": "test4",

  "var5": "test5",

  "var6": "test6",

}

Let me know how should we write???

1 ACCEPTED SOLUTION

This is how I ma doing an it works fine. Make sure to use .toString() while passing the value to JSON.


You can use below script which is much better way to conver to Json and stringify it



var jsonObj = {};


jsonObj.u_name = current.name.toString();


jsonObj.u_application_id = current.u_application_id.toString();


jsonObj.u_approval_group = current.change_control.toString();


jsonObj.u_category = current.category.toString();


jsonObj.u_assignment_group = current.assignment_group.toString();



try {


var r = new sn_ws.RESTMessageV2('CMDB Application Integration', 'POST');


r.setStringParameterNoEscape('content',global.JSON.stringify(jsonObj));


var response = r.execute();


var responseBody = response.getBody();


var httpStatus = response.getStatusCode();


}


catch(ex) {


var message = ex.getMessage();


}



Also activate HTTP log and check your Request body if it is sent correctly.



find_real_file.png



Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

17 REPLIES 17

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Rk,



Sample script


var sm = new sn_ws.RESTMessageV2("Update user","post");


var obj = {'var1' : 'test1', 'var2' : 'test2', 'var3' : 'test3'};


sm.setRequestBody(JSON.stringify(obj)); //setRequestBody(body string);The request body to be send in string format.



Reference:


https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=r_RMV2-RESTMessageV2


https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=r_RMV2-setRequestBody_String_body


Jerry121
Kilo Contributor

How can I send current values???


var obj = {'var1' : current.getValue('PASS FIELD COLUMN NAME'), 'var2' : 'test2', 'var3' : 'test3'};


Jerry121
Kilo Contributor

This is what I am trying bu tit is still not working, I don't know where am i making mistake





try {


var sm = new sn_ws.RESTMessageV2('Test', 'POST');




var obj = {'var1' : 'current.getValue('field1'), 'var2' : 'current.getValue('field2')};


sm.setRequestBody(JSON.stringify(obj)); //




var response = r.execute();


var responseBody = response.getBody();


var httpStatus = response.getStatusCode();


gs.log("Successful" + responseBody +" status "+httpStatus);


}


catch(ex) {


var message = ex.getMessage();


}


}