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

sachin_namjoshi
Kilo Patron
Kilo Patron

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

deepak50
Tera Contributor

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.

Markus Kraus
Kilo Sage

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.

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.