can I change the format of response sent by scripted rest api ?

kumarkamlesh
Mega Contributor

I am using scripted REST api for integration. In POST method, I am processing the data and returning a JSON data. But that data is not reaching the sender which is invoking the POST method. Sender is expecting the response in below format :-

Headers:
Content-type: application/json

Body:
{
"speech": "Barack Hussein Obama II was the 44th and current President of the United States.",
"displayText": "Barack Hussein Obama II was the 44th and current President of the United States, and the first African American to hold the office. Born in Honolulu, Hawaii, Obama is a graduate of Columbia University     and Harvard Law School, where ",
"data": {},
"contextOut": [],
"source": "DuckDuckGo"
}

I have formatted my response in the above format as below :-

response.setBody({"speech": "Barack Hussein Obama II was the 44th and current President of the United States.",
"displayText": "Barack Hussein Obama II was the 44th and current President of the United States, and the first African American to hold the office. Born in Honolulu, Hawaii, Obama is a graduate of Columbia University     and Harvard Law School, where ",
"data": {},
"contextOut": [],
"source": "DuckDuckGo"

});

response.setContentType('application/json');

But it's not working. Reason I believe is, when I checked this with REST api explorer and Postman I found that the response that servicenow sends contains "result" attribute which sender do not recognizes hence ignoring it.

{
  "result": {

"speech": "Barack Hussein Obama II was the 44th and current President of the United States.",
"displayText": "Barack Hussein Obama II was the 44th and current President of the United States, and the first African American to hold the office. Born in Honolulu, Hawaii, Obama is a graduate of Columbia University     and Harvard Law School, where ",
"data": {},
"contextOut": [],
"source": "DuckDuckGo"

}

}

Can we format the responce such that it do not contain "result" in it or what is the other solution for this. Please suggest.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Kamlesh,



So you don't want the result key in the response.


so here you go you need to use getStreamWriter() to remove the "result"



var writer = response.getStreamWriter();



var response_body = {"speech": "Barack Hussein Obama II was the 44th and current President of the United States.",


"displayText": "Barack Hussein Obama II was the 44th and current President of the United States, and the first African American to hold the office. Born in Honolulu, Hawaii, Obama is a graduate of Columbia University     and Harvard Law School, where ",


"data": {},


"contextOut": [],


"source": "DuckDuckGo"


};



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




Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Kamlesh,



So you don't want the result key in the response.


so here you go you need to use getStreamWriter() to remove the "result"



var writer = response.getStreamWriter();



var response_body = {"speech": "Barack Hussein Obama II was the 44th and current President of the United States.",


"displayText": "Barack Hussein Obama II was the 44th and current President of the United States, and the first African American to hold the office. Born in Honolulu, Hawaii, Obama is a graduate of Columbia University     and Harvard Law School, where ",


"data": {},


"contextOut": [],


"source": "DuckDuckGo"


};



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




Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

where do we have to write this code? 

It has to be written in your scripted rest service.

Regards

Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

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"
}
]
}