attachment getting corrupted
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2017 11:22 PM
Hi,
I have created scripted rest api and attaching files through that only, apart from text/plain, excel and xml all the files like pdf, jpeg etc are getting corrupted.
KIndly let me if its something related to UTF-8.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2017 11:34 PM
Hi Sneha,
So you have created scripted rest api and trying to attach it to some table.
Why not use attachment api to attach to record. this is given by ServiceNow out of the box.
Attachment API - POST /now/attachment/upload
Re: Attachment API - POST /now/attachment/file
Attaching JSON in ServiceNow using REST Messages - Dev Tricks
Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2017 11:47 PM
Hello Sneha,
Your Scripted API is like this -
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var hdrs = {},
var requestBody = request.body;
var requestData = requestBody.data; //May be an array or a single object
attachment_sys_id = requestData.attachment_id;
hdrs['Content-Type'] = requestData.content_type;
response.setStatus(200);
response.setHeaders(hdrs);
var writer = response.getStreamWriter();
var attachmentStream = new GlideSysAttachmentInputStream(attachment_sys_id);
writer.writeStream(attachmentStream);
})(request, response);
If then you need to convert output into base64.
Yes it is related to UTF-8 characters.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2017 11:56 PM
Hi Nayan,
I am converting output into bas64 also
////////////////////// create incident with attachment/////////////////////
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var body = request.body.data;
// Define the request parameters with prefix 'req_' and input variables with prefix 'in_'
var in_user_name = body[0].req_user_name;
var in_short_description = body[0].req_short_description;
var in_description = body[0].req_description;
var in_caller_id;
var in_location;
//attachment Inputs
var in_content_type = [];
var in_binary_input = [];
var in_file_name = [];
var in_attach = [];
// Get the requester sys_id and Location ID through user name
var gr1 = new GlideRecord("sys_user");
gr1.get("user_name", in_user_name);
in_caller_id = gr1.sys_id;
// Create record in incident table
var gr = new GlideRecord('incident');
gr.initialize();
gr.caller_id = in_caller_id;
gr.short_description = in_short_description;
gr.u_medium = 'Self-Service';
gr.description = in_description;
gr.comments = in_description;
var rec = gr.insert();
// Create an attachment and associate with incident
for(var i=0; i<body[0].attachment.length; i++) {
// Define attachment Inputs
in_content_type[i] = body[0].attachment[i].req_content_type;
in_binary_input[i] = body[0].attachment[i].req_binary_input;
in_file_name[i] = body[0].attachment[i].req_file_name;
var gt = new GlideRecord("u_gppattachment");
gt.initialize();
gt.u_file_name = in_file_name[i];
gt.u_binary_input = in_binary_input[i];
gt.u_content_type = in_content_type[i];
gt.u_incident_number = rec;
gt.insert();
// new gppattachment().createattach(rec,in_file_name[i],in_binary_input[i],in_content_type[i]);
}
return {
// Define the response parameters with prefix 'res_'
"res_custom_message" : 'Thanks for raising incident',
"res_number" : gr.number.getDisplayValue(),
};
})(request, response);
/////////////////////// create attachment /////////////
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var body = request.body.data;
var in_content_type = body.req_content_type;
var in_binary_input = body.req_binary_input;
var in_file_name = body.req_file_name;
var in_number_id = body.req_number_id;
// Provide User Credentials
var user = 'test';
var password = 'test';
// Encode the File Name
var file = encodeURI(in_file_name);
// Invoke Create Attachment API
var req = new sn_ws.RESTMessageV2();
req.setEndpoint('https://dev-servicedesk.publicisgroupe.net/api/now/attachment/file?table_name=incident&table_sys_id=' + in_number_id + '&file_name=' + file);
req.setHttpMethod('POST');
req.setBasicAuth(user,password);
req.setRequestHeader("Accept","application/json");
req.setRequestHeader('Content-Type',in_content_type);
//Decode the Base 64 Payload received through Rest API
var encData = GlideStringUtil.base64Decode(in_binary_input);
req.setRequestBody(encData);
var res = req.execute();
// creating response parameters for attachment
/* var attachmentob = {};
attachmentob.res_file_name = in_file_name + '';
attachmentob.res_content_type = in_content_type + '';
attachmentob.res_input = in_binary_input + '';
attachmentob.res_body = res.getBody();
return attachmentob; */
return res.getBody();
})(request, response);