Issue with getRequestBody() in Attachment api
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2025 04:03 AM
Attachment api is working fine and returning 201 status code but getRequestBody() always returns null what might be the issue with this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2025 08:28 AM - edited 07-17-2025 08:30 AM
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2025 05:08 PM
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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2025 03:51 AM
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.