How to add a variable to a Rest Message's Content field dynamically?

Nisar2
Mega Guru

I've a rest message function defined with some default variables:

Nisar2_0-1689850491483.png

So via script, I can substitute those values

var rm = new sn_ws.RESTMessageV2('dummyAPI', 'post');
rm.setStringParameter('callback_url', 'xxxxxxxx');
rm.setStringParameter('correlationId', 'yyyyyyy');

But is it possible to add an extra variable and substitute it on the fly? Something like below:

rm.setStringParameter('new_variable', 'abc');

I tried with the above code, but in the scripted rest API resource, when I did

gs.log(JSON.stringify(request.body.data));

I could only see "callbackUrl" and "correlationId" keys and not "new_variable".

 

Is there way to have that added? I don't want to update the rest message's "Content" field and would like to have that extra variable added by script.

5 REPLIES 5

Geoff_T
Mega Sage

In what scenario would you need an extra variable added. Surely the request body would be pre-defined with mandatory / mandatory values?

Dev vs Prod. When triggering in dev environment, I need to add an extra variable for aiding quick testing but when it's pushed to prod the variable is not required. I can simply use instance_name sys_property to know which environment the code is running in. But the only thing I need to know if it's possible to do this.

I'd probably just create separate REST messages. One for Prod, the other non Prod. Then you can call whichever one you want based on the instance_name.

 

var env = gs.getProperty('instance_name');
var restMessage = env == 'prod' ? 'dummyAPIProd' : 'dummyAPINonProd';

var rm = new sn_ws.RESTMessageV2(restMessage, 'post');
rm.setStringParameter('callback_url', 'xxxxxxxx');
rm.setStringParameter('correlationId', 'yyyyyyy');

if(env == 'prod')
rm.setStringParameter('new_variable', 'abc');

 

Yes, this is a solution that we had already considered. But your answers seem to suggest I cannot do this via script?