- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2022 09:25 AM
Hi,
I am using sn_ws.RESTMessageV2 in script to read response from one endpoint as below:
var r = new sn_ws.RESTMessageV2('messagename','httpmethod');
var response = r.execute();
var responseBody = response.getBody();
response is in array.
I am getting response body if there less number of records , eg 20 records in array.
but when there are more number of records I am getting response body as blank in script, although I can see it in test http method.
Is there a way to get large response body in script , http method is GET.
Thanks
Deepak
Solved! Go to Solution.
- Labels:
-
Integrations
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2022 05:14 PM
Hi Deepak,
Unfortunately, RESTResponseV2 doesn't have getResponseAsStream() method.
So have to use .saveResponseBodyAsAttachment() method. This will require a table and a record to attach the attachment.
var tablename = '<table name>';
var recordSysId = '<record in table to attach file>';
var filename = '<filename>';
var request = new sn_ws.RESTMessageV2('messagename','httpmethod');
request.saveResponseBodyAsAttachment(tablename, recordSysId, filename);
response = request.execute();
httpResponseStatus = response.getStatusCode();
Then if necessary use GlideSysAttachment() method to read attachment as a stream to read the content to use in a script. "attachmentSysId" is the sys_id of the file in the sys_attachment table. .saveResponseBodyAsAttachment() method, unfortunately, doesn't return the sys_id so the table needs to be queried using tablename, recordsysId, and filename to find it.
Furthermore, there is a maximum size of attachment.
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0718101
and for REST API
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0747638
In all, it is easier to use paging if the REST API at the other site supports it.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2022 09:28 AM
Is the output of external API is in JSON?
You can will have parse response from JSON and then print values you want to use from JSON response.
Regards,
Sachin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2022 10:41 AM
response is in string. parsing is not the issue.
Issue is when response is of large size , response body is showing as blank. so there is nothing to parse.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2022 11:38 AM
ServiceNow recommends to use 'executeAsync'. This will put your request into the ecc_queue (output queue) and you will see your response there. Please try this and report back, if you get any results that way.
If the payload is larger than a certain threshold, it will be put into a file attached to the input queue where it then can be process further.
If you still do not get any results, the problem might be on the endpoint-side. If you do get results please answer and i will proceed to give some example processing scripts.
EDIT: Another option might be saveResponseBodyAsAttachment (Official Documentation) - this might also help.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2022 05:14 PM
Hi Deepak,
Unfortunately, RESTResponseV2 doesn't have getResponseAsStream() method.
So have to use .saveResponseBodyAsAttachment() method. This will require a table and a record to attach the attachment.
var tablename = '<table name>';
var recordSysId = '<record in table to attach file>';
var filename = '<filename>';
var request = new sn_ws.RESTMessageV2('messagename','httpmethod');
request.saveResponseBodyAsAttachment(tablename, recordSysId, filename);
response = request.execute();
httpResponseStatus = response.getStatusCode();
Then if necessary use GlideSysAttachment() method to read attachment as a stream to read the content to use in a script. "attachmentSysId" is the sys_id of the file in the sys_attachment table. .saveResponseBodyAsAttachment() method, unfortunately, doesn't return the sys_id so the table needs to be queried using tablename, recordsysId, and filename to find it.
Furthermore, there is a maximum size of attachment.
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0718101
and for REST API
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0747638
In all, it is easier to use paging if the REST API at the other site supports it.