- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2024 11:34 PM
We are integrating ServiceNow with ICC ManageEngine
In it we are attaching attachment to their Request (From our Incident), but they have provided the payload template in python script
aturl = f"https://abc.com/''
fileName = "test.txt" #data.csv
filePath = "E:\\test.txt"
fileType = mimetypes.guess_type(filePath)
files = []
fileObj = ('file0', (fileName,open(filePath,'rb'), fileType))
files.append(fileObj)
data = files
response = requests.put(aturl,headers=headers,files=data,verify=False)
print(response.text)
Like this we don't know
fileObj = ('file0', (fileName,open(filePath,'rb'), fileType))
How to pour the data in above path. In it file0 is static value and rb as well static value
If anyone have done this previously or have dealt with the same thing please suggest what to do.
We have tried sending file Via Postman in their EndPoint and it is going but we can't through REST API.
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
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/
****************************************************************************************************************