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

Can you share a screenshot of Rest message as well?


pradeepksharma can't provide that, but I can answer you if you have any questions.



Rk


how about this one



try {


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




var obj = {'var1' : 'current.variables.getValue('field1'), 'var2' : 'current.variables.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();


}


}


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.

try the below code.


var jsonObj = {};


jsonObj.var1= current.variables.getValue('field1').toString();


jsonObj.var2= current.variables.getValue('field2').toString();



try {


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


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


var response = r.execute();


var responseBody = response.getBody();


var httpStatus = response.getStatusCode();



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


}


catch(ex) {


var message = ex.getMessage();


}



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