Issue with getRequestBody() in Attachment api

Kishor O
Tera Sage

Attachment api is working fine and returning 201 status code but getRequestBody() always returns null what might be the issue with this?

 

KishorO_0-1752750168530.png

 

7 REPLIES 7

@Ankur Bawiskar Yes record is attaching and I can able to open in the other instance.

@Brad Warman Updated code with content-type

var restMessage = new sn_ws.RESTMessageV2();
restMessage.setHttpMethod("post");
restMessage.setEndpoint("https://mytest.service-now.com/api/now/attachment/file");
restMessage.setBasicAuth('SN_INT_USER_IDM_User', 'CRbr#9extusrpr');
restMessage.setQueryParameter("table_name", "incident")
restMessage.setQueryParameter('file_name', 'ODOO and ServiceNow integration (AutoRecovered).docx');
restMessage.setQueryParameter('table_sys_id', '8f9a39a783e66610da0653a8beaad325')
restMessage.setRequestHeader("Content-Type", "docs");
restMessage.setRequestHeader("Accept", "application/json");
restMessage.setRequestBodyFromAttachment("e4fd39afc366221001a098ec0501315e");
var response = restMessage.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var requestBody = restMessage.getRequestBody();
gs.print("status" + " " + httpStatus);
gs.log("requestbody" + " " + requestBody);
gs.print("response body" + " " + responseBody);

My updated code with content-type

The only way I was able to get it to show up in getRequestBody() was to not use the setRequestBodyFromAttachment() method. You can convert the attachment into an input stream object and send that via setRequestBody() if it's supported by the endpoint.

 

My example below is using a pre-configured REST message, but you should be able to set the values manually in the script without using one if needed.

 

var restMessage = new sn_ws.RESTMessageV2("test", "Default POST");
restMessage.setHttpMethod("post");
restMessage.setQueryParameter("table_name", "incident");
restMessage.setQueryParameter('file_name', 'ODOO and ServiceNow integration (AutoRecovered).docx');
restMessage.setQueryParameter('table_sys_id', '8f9a39a783e66610da0653a8beaad325')
restMessage.setRequestHeader("Accept", "application/json");

var attachmentStream = getInputStream('<SYSID OF ATTACHMENT RECORD>');
restMessage.setRequestBody(attachmentStream);

var requestBody = restMessage.getRequestBody();
var response = restMessage.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

gs.info("status" + " " + httpStatus);
gs.info("requestbody" + " " + requestBody);
gs.info("response body" + " " + responseBody);

function getInputStream(input) {
	var inputStream = new GlideSysAttachment().getContentStream(input);
	return inputStream;
}

 

Samuel Salomon
Tera Contributor

Why do you need to call getRequestBody? Is there any comparison required?

As this is an attachment and getRequestBody is returning a string i believ it is hard to display it in logs.