Scripted Rest API returns[object Object] how to parse ?

Kiddy
Tera Guru

Couldnt parse [object Object]

 

instance A:

rest api body is set

var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://devxxxxx.service-now.com/api/757899/validatepredeploy');
request.setHttpMethod('POST');


var enc=current.u_enclave;

var user = 'admin';
var password = 'xxxxx';

var body ={};
body.enclave=enc;

request.setBasicAuth(user, password);
request.setRequestHeader("Accept", "application/json");
request.setRequestHeader('Content-Type', 'application/json');
request.setRequestBody(JSON.stringify(body));

var response = request.execute();

 

Instance B: scripted Rest API parsing to get value of enclave

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

// implement resource here

var requestBody = request.body.data;
gs.log('requestJSON enclave 1'+requestBody ,'sh01');
})(request, response);

 

Ouput:

requestJSON enclave 1[object Object]

8 REPLIES 8

Anirudha1
Giga Contributor

Hi Kiddy,

So there are two things:-

1) To retrieve the value of "Enclave" you will need to keep it in JSON format and do gs.log('requestJSON enclave 1 '+requestBody.enclave,'sh02'); 

As of now I do not see a value for enclave in the given screenshot.

2) Once you use stringify you are converting the entire JSON to string so that will just act like string data where data retrieval is difficult.

Thanks i fixed it using below code i think this was givinf me issue

var body ={};
body.enclave=enc;

to

var body ={};
body.enclave=enc+'';

Anirudha1
Giga Contributor
Please mark my response useful if it helped. Thanks 🙂

Anirudha1
Giga Contributor

Great 🙂

Please mark my answer correct if that really helped 🙂

Thanks,

Anirudha