Reading large response body from sn_ws.RESTMessageV2 response in script

deepak50
Tera Contributor

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  

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

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.

https://developer.servicenow.com/dev.do#!/reference/api/sandiego/server/sn_ws-namespace/c_RESTMessag...

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.

https://developer.servicenow.com/dev.do#!/reference/api/sandiego/server/no-namespace/c_GlideTextRead...

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.

View solution in original post

6 REPLIES 6

deepak50
Tera Contributor

Thanks Markus and Hitoshi,


I am trying to use saveResponseBodyAsAttachment as below  to save response as attachemnet.

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();

Here I am able to create attachement in sys_attachment table when tablename=incident and recordSysId =valid sys id.
Instead of incident I am trying to use a custom table (insert a record in custom table then pass table name and sysid of record in args) , but this is not working.

Is there any requirement for table to be used in saveResponseBodyAsAttachment parameters ?

Thanks
Deepak

The regular 'Application Access' rules should apply (Accessible from).

I have tried this with a custom table and I didn't have any problems... can you try gs.info(response.getResponseAttachmentSysid())? Or do you get a certain error message if you run the REST call in a background script?