scripted REST - response body without "result"

RadovanK
Tera Contributor

Hi, 

I created "scripted REST (Inbound POST) " and I need to set the response body to:

{ "ok": true, "stuff": "This is good" }

 

I used this code to "translate"  JSON to JavaScript object because only javascript object is allowed as response body in ServiceNow scripted REST :

var jsBody = JSON.parse('{"ok": true, "stuff": "This is good"}');
response.setBody(jsBody);
 
I tested the result using POSTMAN POST but the response body is:
{
    "result": {
        "ok": true,
        "stuff": "This is good"
    }
}
 
It seems that the Service Now core add the "result" to the response body. But it is bad in my scenario, because  external application doesn't know how to parse it. The  correct result for external application is without "result":
 {"ok": true, "stuff": "This is good"}
 
Thanks for answer,
 
2 REPLIES 2

HrishabhKumar
Kilo Sage

Hi @RadovanK ,

Try this code instead, where you are setting the response body;

  var responseBody = JSON.stringify({
        "ok": true,
        "stuff": "This is good"
    });

 response.getStreamWriter().writeString(responseBody);

 

Thanks

Hi @RadovanK 

If you use response.getStreamWriter().writeString() function, it won't add addition "result" wrapper in your response body.

 

 

 

Thanks

Hope this helps.

If it helped please mark it as helpful.