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

Create JSON request body through Script

RFJ1
Tera Contributor

Hi,

 

I want to create JSON request body through script. Can someone help me with the code?

 

var request = new sn_ws.RESTMessageV2('User Group', 'Group Updation');
request.setStringParameter('sysid' , "a69fc6a21b227dd8df7132e2cd4bcb91");
for (var x in current)
{
 if(current[x] != previous[x])
 request.setRequestBody('{"'x'" : "'+ current.[x]'"}');
}

 

I want to loop through each field and if the value of any field is different, i want to add that field name and its current value to the request body. If the values are hardcoded string we can use '\' character but how can we pass variable names and its value in the request body.

 

Thanks in advance. 

15 REPLIES 15

RFJ1
Tera Contributor

Hi @Jim Coyne ,

 

Tried  getValue() even replaced escape characters still getting 400 error.

 

RFJ1_0-1700506561589.png

 

Hi @Anand Kumar P ,

 

When I am sending description data i am getting a 400 response saying that my payload is not valid JSON. Can you please help me,

 request.setStringParameter('description', current.description);

Hi @RFJ1,

Use below script 

if (current.description) {
requestBody.description = current.description;
}

var requestBodyString = JSON.stringify(requestBody);
request.setRequestBody(requestBodyString);

 

Thanks,

Anand

 

 

Hi @Anand Kumar P ,

 

But this is a different post method and I am using variable substitutions instead of directly setting with script.

 

Here is the script I am using for this POST,

 

 try 
		{
            var request = new sn_ws.RESTMessageV2('User Group', 'Group Creation');
            request.setStringParameter('parent', current.parent);
            request.setStringParameter('description', current.description);
            request.setStringParameter('sys_updated_on', current.sys_updated_on);
            request.setStringParameter('type', current.type);
            request.setStringParameter('u_ticket_destination', current.u_ticket_destination);
            request.setStringParameter('sys_id', current.sys_id);
            request.setStringParameter('sys_updated_by', current.sys_updated_by);
            request.setStringParameter('sys_created_on', current.sys_created_on);
            request.setStringParameter('email', current.email);
            request.setStringParameter('manager', current.manager);
            request.setStringParameter('name', current.name);
            var response = request.execute();
            var responseBody = response.getBody();
            gs.info("Rest Group Updation :" + responseBody);
            var httpStatus = response.getStatusCode();
            gs.info("Rest Group Updation :" + httpStatus);
        } 
		catch (ex) 
		{
            var message = ex.message;
            gs.info("Rest Group Updation :" + message);
        }
    }

 

This works fine as long as I don't give multi line description. When I give my description like below, I get 400 error.

 

RFJ1_0-1700502918677.png

 

 

You will want to encode that data using:

request.setStringParameter('description', encodeURIComponent(current.getValue("description")));

The "encodeURIComponent" function will encode any special characters, like your line breaks.