REST API Post - JSON content type

vinitha3
Tera Guru

Hello,

This is a part of my JSON message which accepts array input

{

"value": {"array": { "elements": [

{"number": {"value": 80}},

{"number": { "value": 100 }}

]}

},

"type": "Array/number",

"name": "cdmAddDisk",

"scope": "local"

}

The value I have to pass here comes from a Request Item multi line text separated by commas.

But it does not accept it. Any idea?

Thanks,

Vinitha.K

1 ACCEPTED SOLUTION

vinitha3
Tera Guru

I got it by setting r.setStringParameterNoEscape();



r.setStringParameterNoEscape("cdmAddDisk",global.JSON.stringify(str.split(',')).replace(']',',]').replace(/\"/g,"{\"number\": {\"value\":").replace(/{\"number\": {\"value\":,/g,"}},").replace(',]',']'));



Thank you so much for the help.


View solution in original post

14 REPLIES 14

You can change the above code in below way to get the ouptput in your format...try below in backgroup scritp and see the output.



var str = '80,100';


gs.print(global.JSON.stringify(str.split(',')).replace(']',',]').replace(/\"/g,"{\"number\": {\"value\":").replace(/{\"number\": {\"value\":,/g,"}},").replace(',]',']'));


r.setStringParameter("cdmAddDisk",global.JSON.stringify(str.split(',')).replace(']',',]').replace(/\"/g,"{\"number\": {\"value\":").replace(/{\"number\": {\"value\":,/g,"}},").replace(',]',']'));



If I set in the script like this, I get the same 400 error again.



https://virtualviking.net/2015/06/15/using-servicenow-as-a-front-end-for-vrealize-automation/


Can u pls share you script and the complete error message ur getting..


find_real_file.png


    I am getting the values through request item variables and passing it to JSON message(I have already shared).



    script to pass the RITM variables to POST script,


r.setStringParameter("cdmBusinessJustification", current.variables.BusinessJustification);


r.setStringParameter("cdmAddDisk",global.JSON.stringify(str.split(',')).replace(']',',]').replace(/\"/g,"{\"number\": {\"value\":").replace(/{\"number\": {\"value\":,/g,"}},").replace(',]',']'));


r.setStringParameter("cdmPowerStartTime", current.variables.PowerStartTime);


r.setStringParameter("cdmPowerStopTime",current.variables.PowerStopTime);


r.setStringParameter("cdmPowerWeekends", current.variables.PowerWeekends);


Ok got it....     so the json string building here is getting replace with your REST post content variable "cdmAddDisk". I suspect issue is with that binding (one small char in between can make this a bad request 400 error). Can u get the output from background script and hardcore the same along with hard coded values for other fields, validate that content with any online json validator site and test from the rest post.....see if you   are getting the same error.