- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2024 03:27 AM
Hello All,
We are doing third party integration with one tool where they accept Multipart form-data.
With the script which I'm providing below we are able to share text format files and getting 201 response, but when I try to send text format files it shows 400 something error in HTTP outbound.
If you'll be able to help with this one it will be really big help.
Business rule :-
(function executeRule(current, previous /*null when async*/ ) {
try {
var boundary = "----WebKitFormBoundary" + gs.generateGUID();
var gsa = GlideSysAttachmentInputStream(current.sys_id.toString());
var baos = new Packages.java.io.ByteArrayOutputStream();
gsa.writeTo(baos, 0, 0);
baos.close();
var encodedFile = GlideStringUtil.base64Encode(baos.toByteArray());
var contentType = current.content_type;
var fileName = current.file_name;
// Construct the dynamic request body with the generated boundary
var requestBody = "--" + boundary + "\r\n" +
"Content-Disposition: form-data; name=\"input_file\"; filename=\"" + fileName + "\"\r\n" +
"Content-Type: " + contentType + "\r\n\r\n" +
encodedFile + "\r\n" +
"--" + boundary + "--";
var restMessage = new sn_ws.RESTMessageV2();
restMessage.setEndpoint('https://abc.com/attachment/upload');
restMessage.setHttpMethod("PUT");
restMessage.setRequestHeader("TECHNICIAN_KEY", "XXXX-1B73-40C9-8E3A-A8807CE53DCF");
restMessage.setRequestHeader("accept", "application/json");
restMessage.setRequestHeader("content-type", 'multipart/form-data; boundary=' + boundary);
restMessage.setRequestHeader("Content-disposition", "application/json");
restMessage.setRequestBody(requestBody);
var response = restMessage.execute();
var httpStatus = response.getStatusCode();
var responseBody = response.getBody();
} catch (ex) {
gs.log("Error sending attachment to ManageEngine: " + ex.message);
}
})(current, previous);
This happens if I send "encodedFile" (That is base64) this variable in requestBody variable.
But when I send "baos" (That is bytes) in 'requestBody' both file goes but the image format file shows blank on third party side instance.
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Akshay Kamble (SD_Akshay)
ServiceNow Developer
LinkedIn: https://www.linkedin.com/in/akshay-kamble-1504/
****************************************************************************************************************
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 weeks ago
We finally were able to solve this issue. We created the below Business Rule on Incident table
Table :- Incident
When : After Update
(function executeRule(current, previous /*null when async*/ ) {
var grGetAttach = new GlideRecord("sys_attachment");
grGetAttach.addQuery('table_sys_id', current.sys_id);
grGetAttach.query();
while (grGetAttach.next()) {
try {
var json = new JSON();
var obj = {},
data;
obj.note = {};
obj.note.description = 'Attchment Created';
obj.note.show_to_requester = true;
obj.note.mark_first_response = false;
obj.note.add_to_linked_requests = true;
data = json.encode(obj);
var r = new sn_ws.RESTMessageV2('ME Integration', 'Create_Notes');
r.setRequestHeader("TECHNICIAN_KEY", "0CE7xxxxxxxxxxxxxxxxxxxxxxDCF");
r.setStringParameterNoEscape('request_id', current.correlation_id.toString());
r.setQueryParameter("input_data", data);
r.setStringParameterNoEscape('request_id', current.correlation_id.toString());
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var result;
try {
result=JSON.parse(responseBody);
}catch(parseError){
gs.log("Error: parsing Create notes resonse: " + parseError.message, " ME Integration");
continue;
}
if(!result || !result.note || !result.note.id){
gs.log("Error: Could not retrive note ID from Create_notes APIresponse. skipping attachment");
continue;
}
var noteId =result.note.id.toString();
/*------------------------------------------------------------------------------------------------------------------------------------*/
var boundary = "----WebKitFormBoundary" + gs.generateGUID();
var bodyList = [
'--' + boundary,
'Content-Disposition: form-data; name="input_file"; filename="' + grGetAttach.file_name + '"',
'Content-Type: ' + grGetAttach.content_type,
'',
function(byteOutStrm) {
var attachmentStream = new GlideSysAttachmentInputStream(grGetAttach.sys_id);
attachmentStream.writeTo(byteOutStrm, 0, 0);
},
'',
'--' + boundary + '--'
];
var byteOutStrm = new Packages.java.io.ByteArrayOutputStream();
for (var i = 0; i < bodyList.length; i++) {
if (typeof bodyList[i] !== 'function') {
var bytes = Packages.java.lang.String('' + bodyList[i] + '\r\n').getBytes();
byteOutStrm.write(bytes, 0, bytes.length);
} else {
bodyList[i].call(this, byteOutStrm);
}
}
var byteArray = byteOutStrm.toByteArray();
var poll = new GlideRecord('sys_poll');
if (!poll.get('message', 'Temporary attachments for file transfers')) {
poll.initialize();
poll.message = 'Temporary attachments for file transfers';
poll.insert();
}
var attachment = new GlideSysAttachment();
attachment.deleteAll(poll);
var attachmentSysID = attachment.write(poll, 'PostBody', 'application/octet-stream', byteArray);
var restMessage = new sn_ws.RESTMessageV2('ME Integration', 'send_attachment');
restMessage.setHttpMethod("PUT");
restMessage.setRequestHeader("TECHNICIAN_KEY", "0CExxxxxxxxxxxxxxxxxxxxxxxxxxxxDCF");
restMessage.setRequestHeader("Accept", "*/*");
restMessage.setRequestHeader("Content-Type", 'multipart/form-data; boundary=' + boundary);
restMessage.setStringParameterNoEscape('request_id', current.correlation_id.toString());
restMessage.setStringParameterNoEscape('notes_id', noteId);
restMessage.setRequestBodyFromAttachment(attachmentSysID);
var response1 = restMessage.execute();
var httpStatus1 = response1.getStatusCode();
var responseBody1 = response1.getBody();
gs.log("ManageEngine Upload Response Body: " + responseBody1, "ME Integration");
gs.log("ManageEngine Upload Response Status: " + httpStatus1, "ME Integration");
} catch (ex) {
gs.log("Error sending attachment to ManageEngine: " + ex.message);
}
}
})(current, previous);
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Akshay Kamble (SD_Akshay)
ServiceNow Developer
LinkedIn: https://www.linkedin.com/in/akshay-kamble-1504/
****************************************************************************************************************

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2024 03:36 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 weeks ago
We finally were able to solve this issue. We created the below Business Rule on Incident table
Table :- Incident
When : After Update
(function executeRule(current, previous /*null when async*/ ) {
var grGetAttach = new GlideRecord("sys_attachment");
grGetAttach.addQuery('table_sys_id', current.sys_id);
grGetAttach.query();
while (grGetAttach.next()) {
try {
var json = new JSON();
var obj = {},
data;
obj.note = {};
obj.note.description = 'Attchment Created';
obj.note.show_to_requester = true;
obj.note.mark_first_response = false;
obj.note.add_to_linked_requests = true;
data = json.encode(obj);
var r = new sn_ws.RESTMessageV2('ME Integration', 'Create_Notes');
r.setRequestHeader("TECHNICIAN_KEY", "0CE7xxxxxxxxxxxxxxxxxxxxxxDCF");
r.setStringParameterNoEscape('request_id', current.correlation_id.toString());
r.setQueryParameter("input_data", data);
r.setStringParameterNoEscape('request_id', current.correlation_id.toString());
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var result;
try {
result=JSON.parse(responseBody);
}catch(parseError){
gs.log("Error: parsing Create notes resonse: " + parseError.message, " ME Integration");
continue;
}
if(!result || !result.note || !result.note.id){
gs.log("Error: Could not retrive note ID from Create_notes APIresponse. skipping attachment");
continue;
}
var noteId =result.note.id.toString();
/*------------------------------------------------------------------------------------------------------------------------------------*/
var boundary = "----WebKitFormBoundary" + gs.generateGUID();
var bodyList = [
'--' + boundary,
'Content-Disposition: form-data; name="input_file"; filename="' + grGetAttach.file_name + '"',
'Content-Type: ' + grGetAttach.content_type,
'',
function(byteOutStrm) {
var attachmentStream = new GlideSysAttachmentInputStream(grGetAttach.sys_id);
attachmentStream.writeTo(byteOutStrm, 0, 0);
},
'',
'--' + boundary + '--'
];
var byteOutStrm = new Packages.java.io.ByteArrayOutputStream();
for (var i = 0; i < bodyList.length; i++) {
if (typeof bodyList[i] !== 'function') {
var bytes = Packages.java.lang.String('' + bodyList[i] + '\r\n').getBytes();
byteOutStrm.write(bytes, 0, bytes.length);
} else {
bodyList[i].call(this, byteOutStrm);
}
}
var byteArray = byteOutStrm.toByteArray();
var poll = new GlideRecord('sys_poll');
if (!poll.get('message', 'Temporary attachments for file transfers')) {
poll.initialize();
poll.message = 'Temporary attachments for file transfers';
poll.insert();
}
var attachment = new GlideSysAttachment();
attachment.deleteAll(poll);
var attachmentSysID = attachment.write(poll, 'PostBody', 'application/octet-stream', byteArray);
var restMessage = new sn_ws.RESTMessageV2('ME Integration', 'send_attachment');
restMessage.setHttpMethod("PUT");
restMessage.setRequestHeader("TECHNICIAN_KEY", "0CExxxxxxxxxxxxxxxxxxxxxxxxxxxxDCF");
restMessage.setRequestHeader("Accept", "*/*");
restMessage.setRequestHeader("Content-Type", 'multipart/form-data; boundary=' + boundary);
restMessage.setStringParameterNoEscape('request_id', current.correlation_id.toString());
restMessage.setStringParameterNoEscape('notes_id', noteId);
restMessage.setRequestBodyFromAttachment(attachmentSysID);
var response1 = restMessage.execute();
var httpStatus1 = response1.getStatusCode();
var responseBody1 = response1.getBody();
gs.log("ManageEngine Upload Response Body: " + responseBody1, "ME Integration");
gs.log("ManageEngine Upload Response Status: " + httpStatus1, "ME Integration");
} catch (ex) {
gs.log("Error sending attachment to ManageEngine: " + ex.message);
}
}
})(current, previous);
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Akshay Kamble (SD_Akshay)
ServiceNow Developer
LinkedIn: https://www.linkedin.com/in/akshay-kamble-1504/
****************************************************************************************************************