Need help on passing key value pairs in Rest Post method

divyalakshmi
Tera Guru

Hi,

I have a requirement where whenever a record is created in a particular table (eg incident), i should pass the ticket number and other fields in key value pair like below in rest.

{

  "incidentid": "INC00101",

  "properties": [

      {

          "code": "summary",

          "value": "test incident"

      },

{

"code":"description",

"value":"test desc"

},      

  ]

}

I have configured the Rest Outbound and the outbound requires only two parameters "incidentid" and "properties".

here Properties should push all the filled field names and values. i have written a BR on this table to call the rest outbound and trying to pass summary and application name in key value pair

var property=[];

var json = new JSON();

var r = new sn_ws.RESTMessageV2(incidentRestMessage, 'post');

r.setStringParameter('incidentid', current.number);

//summary

if(current.short_description)

{

var summary={"code":"Summary","value":current.short_description.toString()};

property.push(summary);

}

//ApplicationCode

if(current.cmdb_ci)

{

var ci={"code":"ApplicationCode","value":current.cmdb_ci.getDisplayValue().toString()};

property.push(ci);

}

r.setStringParameter('properties',JSON.stringify(property));

var response = r.execute();

var responseBody = response.getBody();

var rBody = r.getRequestBody();

gs.log("rest call for "+current.number +"   "+rBody); If i try to log the request body to check if the input parameters are correct i get the below log

rest call for INC00101{"incidentid": "INC00101","properties":"[{"code":"Summary","value":"heloo"},{"code":"ApplicationCode","value":"Fire TOOL"}]"}.

i am not sure why this " comes in place of " ". can anyone help me on achieving this ?

Thanks,

Divya Lakshmi.

1 ACCEPTED SOLUTION

Gaurav Bajaj
Kilo Sage

Hi Divya,



Did you try setting parameter with


setStringParameterNoEscape('properties',JSON.stringify(property));



Thanks


Gaurav


View solution in original post

5 REPLIES 5

Gaurav Bajaj
Kilo Sage

Hi Divya,



Did you try setting parameter with


setStringParameterNoEscape('properties',JSON.stringify(property));



Thanks


Gaurav


Hi Gaurav,


After using setStringParameterNoEscape The log returns as {"incidentid": "INC00105","properties":"[{"code":"Summary","value":"heloo"}]"}. But my end tool doesnot accept the quote between properties string and values.


the highlighted quotes should not be sent when passing the parameters. It shoul dbe like {"incidentid": "INC00105","properties":[{"code":"Summary","value":"heloo"}]"}. When i log only JSON.stringfy(property) it returns without quotes but when i render with setStringParameterNoEscape it adds the quotes. Kindly help me on this.


HI Divya,



Please try to remove such undesired quotes by indexOf and subString methods from javascript.



Let me know if would need help with the script.




Please mark correct/helpful based on the impact of the response.




Thanks


Gaurav


The problem is with the highlighted quotes


"properties":


"

[{"code":"Summary","value":"heloo"}]


"

}


this gets added to the parameter after it is rendered with r.setStringParameterNoEscape. Can you help me with the script?


Thanks,


Divya