The CreatorCon Call for Content is officially open! Get started here.

Passing a JSON.stringify object to RESTMessageV2 in Service Portal Widget

dprince87
Kilo Explorer

Hello,

I have a need to pass a JSON string to a REST API I have. I construct a normal javascript object and want to stringy it. But stringify doesn't seem to work. It does not properly encode the string:

var request_body = {"var" : "Hello World"};

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

          r.setStringParameter('body', JSON.stringify(request_obj));

 

var response = r.execute();

is there a limitation here?

Looking at the console logs shows as an object when I do:

        console.log(JSON.stringify(request_body));

But when I concat it with "My Object:" it encodes properly.

        console.log("My Object :" + JSON.stringify(request_body));

Also putting params in the Rest Message does not work because if the variable contains " (double-quote) it breaks the Json string.

Thanks,

2 REPLIES 2

Geoffrey2
ServiceNow Employee
ServiceNow Employee

If your widget is in a Scoped Application then you will need to use global.JSON.stringify(request_obj).


I am getting an issue when I either JSON.stringify or global.JSON.stringify it converts the current.number or what ever variab;e to "variable":{} instead of "variable":"somevalue"

any ideas pls?