Scripted Rest API — any way to get rid of the "result"  wrapper?

Joey Day
Giga Guru

When writing my own Scripted REST API, in the Scripted API Resource record, I'm specifying the object I'd like serialized and returned to the requestor. I've tried writing the Scripted API Resource script two different ways, like this:

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

        // implement resource here

        return {

                  "text": "Hello world!"

        };

})(request, response);

And alternatively like this:

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

        // implement resource here

        response.setBody({

                  "text": "Hello world!"

        });

})(request, response);

But, either way, ServiceNow wraps that returned object in another object containing a single key/value pair named "result" and the value being the object I returned. So, the serialized JSON ServiceNow actually sends back looks like this:

{"result":{"text":"Hello world!"}}

Is there any way to get rid of this "result" wrapper? I didn't write the system that'll receive this response and I need the returned JSON to be formatted to their strict specification or my integration won't work.

1 ACCEPTED SOLUTION

dave_slusher
ServiceNow Employee
ServiceNow Employee

In the circumstance where you want full control,   you can do response.getStreamWriter() which will give you that object.   Write your response to that and it won't be wrapped in the result object.  



The downside is that it won't automatically response to the Accept headers when you do that.   If that is important,   you'll have to branch on them inside your code,   writing the JSON string one way or the XML string the other.



Make sense?


View solution in original post

7 REPLIES 7

dave_slusher
ServiceNow Employee
ServiceNow Employee

In the circumstance where you want full control,   you can do response.getStreamWriter() which will give you that object.   Write your response to that and it won't be wrapped in the result object.  



The downside is that it won't automatically response to the Accept headers when you do that.   If that is important,   you'll have to branch on them inside your code,   writing the JSON string one way or the XML string the other.



Make sense?


Dave, thanks for pointing me in the right direction. For anyone else who stumbles on this wondering how they can do this, here's my code now:



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


       


        response.setContentType('application/json');


        response.setStatus(200);


        var writer = response.getStreamWriter();


       


        var response_body = {


                  "text": "Hello world!"


        };


       


        writer.writeString(JSON.stringify(response_body));


       


})(request, response);



Obviously you'll want to customize this to your needs, but this is the basic starting point. Now it just returns the JSON I expected:



{"text":"Hello World!"}


Dear Dave,

 

I am also recieving the output with result wrapper for the below REST API script. Please let what modification to be done to remove "result" from the response.

 

Response: 

<response>
<result>
<Email>aaa@example.com</Email>
<Name>AAA ZZZ</Name>
</result>
</response>

 

 

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


var queryParams = request.queryParams;
var isActiveQuery = queryParams.active; //false
var employee_number = queryParams.employeenumber; //'now'

var user_id = employee_number;

var body = {};

var gr = new GlideRecord('sys_user');
gr.addQuery('user_name', user_id);
gr.query();
if(gr.next())
{

body.Name = gr.name;
body.Email = gr.email;
}


response.setBody(body);

//response.setContentType('application/json');
//response.setStatus(200);
//var writer = response.getStreamWriter();
//writer.writeString(global.JSOM.Stringify(body));

})(request, response);

Hello,


I need to create a Webservice API where input is: Sys ID of any record and output is: all *related* active records to that sys id. The relationships between the tables is maintained in Relationships under system definition.


Could you please guide me how to implement this? Sample code would be great.


Input: Sys ID of the record
Output: All *RELATED* active records from *VARIOUS* tables (in the following JSON format):
{
"result": [
{
"Sys ID": "5520267",
"CI Name": "Record 1",
"Table Name": "u_table_a"
},
{
"Sys ID": "5520367",
"CI Name": "Record 2",
"Table Name": "u_table_a"
},
{
"Sys ID": "8331210",
"CI Name": "Record 1",
"Table Name": "u_table_b"
},
{
"Sys ID": "8321210",
"CI Name": "Record 2",
"Table Name": "u_table_b"
},
{
"Sys ID": "3042006",
"CI Name": "Record 3",
"Table Name": "u_table_b"
},
{
"Sys ID": "4509847",
"CI Name": "Record 1",
"Table Name": ""u_table_c"
}
{
"Sys ID": "4509247",
"CI Name": "Record 2",
"Table Name": ""u_table_c"
}
]
}