Scripted REST API to upload file - issue with image
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2025 06:01 PM
Hi there,
I am doing a POC for an end point that should enable us to sync attachment via REST API. The REST API will be used by multiple clients e.g. another ServiceNow instance, Jira etc. We need control on the attachment and we need to do logging, creating incident etc therefore the OOTB attachment end points may not be suitable. So few things to confirm please:
1. Has anyone done this before and are there challenges?
2. I have tried multiple file types e.g. .txt, .docx, .xlsx, .zip, .json, .xml etc and I am at least able to access the body of the request and the data within the Scripted Rest API. However, for the .png files the body returns null. Please see the sample code below. For the image, I have tried request.body.data, request.body.dataStream, request.body.dataStringString but none of them return anything?
Any feedback will be really appreciated.
many thanks
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
try {
var headersString = JSON.stringify(request.headers);
//var bodyString = JSON.stringify(request.body.data);
var body = null;
if (isPlainText(request.headers)) {
var stream = request.body.dataStream;
var reader = new GlideTextReader(stream);
var input = "";
var ln = "";
while ((ln = reader.readLine()) != null) {
input += ln;
}
gs.info("VF: input: " + input);
body = input;
}else if (isImage(request.headers)){
//body = GlideStringUtil.base64DecodeAsBytes(request.body.dataStream);
body = request.body.dataStringstring;
} else {
body = GlideStringUtil.getStringFromStream(request.body.dataStream);
}
// implement resource here
// gs.info("VF: request body: " + bodyString);
gs.info("VF: request body: " + body);
gs.info("VF: request header: " + headersString);
response.setStatus(200);
response.setBody({
message: "Success"
});
} catch (e) {
response.setStatus(500);
response.setBody({
message: "There was error parsing the attachment." + e
});
}
function isPlainText(headers) {
if (request.headers["content-type"] && request.headers["content-type"] == "text/plain") {
gs.log("VF: text/plain content type");
return true;
}
return false;
}
function isImage(headers) {
if (request.headers["content-type"] && request.headers["content-type"] == "image/png") {
return true;
}
return false;
}
})(request, response);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2025 07:24 PM
Good day!
You can try to use the GlideSysAttachment API:
1. Returns the attachment content as a string with base64 encoding.
Function: getContentBase64(GlideRecord sysAttachment)
var attachment = new GlideSysAttachment();
var agr = attachment.getAttachments('incident', '78271e1347c12200e0ef563dbb9a7109'); //create attachment GlideRecord
while(agr.next()) { //for each attachment on the incident record
gs.info(agr.getValue('file_name')); //print file name of attachment
var attachmentContent = attachment.getContentBase64(agr);
gs.info('Attachment content: ' + attachmentContent); //print attachment content
}
Output:
SSBhbSB0ZXh0IGluIGEgdHh0IGZpbGUgYXR0YWNoZWQgdG8gYSByZWNvcmQuIA0
Function: writeBase64(GlideRecord now_GR, String fileName, String contentType, String content_base64Encoded)
var attachment = new GlideSysAttachment();
var rec = new GlideRecord('incident');
var incidentSysID = 'ab1b30031b04ec101363ff37dc4bcbfc';
rec.get(incidentSysID);
var fileName = 'example.txt';
var contentType = 'text/csv';
var base64Encodedcontent = 'SSBhbSB0ZXh0Lg==';
var agr = attachment.writeBase64(rec, fileName, contentType, base64Encodedcontent);
gs.info('The attachment sys_id is: ' + agr);
Output: sys_id of the attachment in sys_attachment table
10cde9971b0820501363ff37dc4bcba6
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2025 07:48 PM
Hello,
Not sure what do you mean. I am not sure if you understand my question at all. There is no incident, no sys_id here yet. The 3rd party is sending Attachments to this end point and attachment could be of any type and I need to access the data from body and store it into sys_attachment.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2025 09:10 PM
Hello @Rahman3
Did you Change rhe content type to "Application/png" and try this ?
Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket.
Regards,
Shivalika
My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194
My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY