GLPI ITSM - ATTACHMENT INTEGRATION

BRUNO BANDEIRA
Tera Guru

Hello guys!

 

I am facing a issue regarding sending attachments from ServiceNow to the GLPI ITSM. Via POSTMAN it all works just fine, but when I've tried to do the same internally with ServiceNow, the file arrives in the other side, mas, in a corrupted manner or it just have the content 'Undefined' in it.

 

If someone could help me out it will be very nice! I put the script that I use in the SCRIPTS BACKGORUND bellow and the POSTMAN screenshots in attachment

 

var token = '{{token}}';
var record = '{{record}}';
var endpoint = '{{endpoint}}';
var appToken = '{{apptoken}}';
var ticket = '{{ticket}}';

var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_sys_id', record);
attachmentGR.query();
if(attachmentGR.next()) {
var attachmentName = attachmentGR.file_name.toString();
var attachmentType = attachmentGR.content_type;
}
 
var grAttachment = new GlideSysAttachment();
var bytes = grAttachment.getBytes(attachmentGR);
 
var boundary = "----" + gs.generateGUID().substring(016);
var CRLF = "\r\n";

var payload = "";
payload += "--" + boundary + CRLF;
payload += 'Content-Disposition: form-data; name="uploadManifest"' + CRLF + CRLF;
payload += JSON.stringify({
    input: {
        name: attachmentName,
        _filename: [attachmentName]
    }
}) + CRLF;

payload += "--" + boundary + CRLF;
payload += 'Content-Disposition: form-data; name="filename"; filename="' + attachmentName + '"' + CRLF;
payload += "Content-Type: " + attachmentType + CRLF;
payload += bytes;

payload += "--" + boundary + "--" + CRLF;

var request = new sn_ws.RESTMessageV2();
request.setEndpoint(endpoint + '/apirest.php/Document/');
request.setHttpMethod('POST');
request.setRequestHeader("App-Token", appToken);
request.setRequestHeader("Session-Token", token);
request.setRequestHeader("Content-Type""multipart/form-data; boundary=" + boundary);
request.setLogLevel('all');
request.setRequestBody(payload);

var sentBody = request.getRequestBody();
var response = request.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
 
if (responseBody.includes('SESSION_TOKEN_INVALID')) {
     response = request.execute();
      httpStatus = response.getStatusCode();
       responseBody = response.getBody();
}

var docJSON = JSON.parse(responseBody);
var docID = docJSON.id; // Captura o documento (DOCUMENT ID)

gs.info('RESPONSE BODY: ' + responseBody);
gs.info('HTTP STATUS: ' + httpStatus);
gs.info('DOCUMENT ID: ' + docID);
 
In some instances, I have received the message that I paste bellow in the Response Body
{"id":11057,"message":"Item adicionado com sucesso: notepad.txt","upload_result":{"filename":[{"name":"681bb8f71971e2.23658968notepad.txt","size":0,"type":"","error":"O arquivo enviado foi parcialmente enviado","prefix":"681bb8f71971e2.23658968","display":"notepad.txt","filesize":"0 B","id":"docfilename1027737742"}]}}

 

1 ACCEPTED SOLUTION

dougmoto
Tera Contributor

From what I remember, you can't set the bytes of the image directly into the form body using ServiceNow's API, instead, you can try using the method:

request.setRequestBodyFromAttachment('<attachment sys_id>');

RESTMessageV2 - setRequestBodyFromAttachment(String attachmentSysId)  

This may simplify your code since you don't have to create the form yourself, but it also may limit the ability to customize the form to fit your endpoint.

View solution in original post

2 REPLIES 2

dougmoto
Tera Contributor

From what I remember, you can't set the bytes of the image directly into the form body using ServiceNow's API, instead, you can try using the method:

request.setRequestBodyFromAttachment('<attachment sys_id>');

RESTMessageV2 - setRequestBodyFromAttachment(String attachmentSysId)  

This may simplify your code since you don't have to create the form yourself, but it also may limit the ability to customize the form to fit your endpoint.

BRUNO BANDEIRA
Tera Guru

I could provide a solution making a mix of both techniques.

 

Thank you for your suport!