How to Send Attachments using REST

  • KM03650660
  • 08-Jun-2020
  • 08-Jun-2020

Summary

How to Send Attachments using REST

Question

When it is necessary to send information from Service Manager using REST, Attachments is also important. The following script is an example that will help to understand how it works. Create a new ScriptLibrary record using the code and test based on this considerations:

1. The incident number can be changed and make sure it has an attachment

2. The name of the Attachment is "myatt0.log" it can be changed and taken from the SYSATTACHMENT table

3. The script only works for a single attachment, if the record has more than one attachment, the script should be adjusted

4. If there is any problem with the Script, contact support, but take into account that this is a tailoring that falls out of support scope

Answer

var incidentRec = new SCFile("probsummary")
var rcinc = incidentRec.doSelect("number=\"IM20280\"")
if (rcinc == RC_SUCCESS)
{
 attc=incidentRec.getAttachments()
 var taskAttachId = attc[0]['href'];
 
 response = doAttachOutboundRouter("POST","http://localhost:9510/SM/9/rest/CEIncidentsPush/IM20280/attachments/",attc[0].value,20,attc,null);
}

function doAttachOutboundRouter(httpAction,url,binaryData,timeout,attachmentObj,param) {
 var response="";
 var tranByMulti = false;
 var boundaryStr = "";

 var headers=[]
 httpHeader = basicAuth(headers, "falconcaseexchange", "passw0rd");
 headers.push(new Header('Content-Type', 'application/json;charset=UTF-8'));
 headers.push(new Header("Content-Type", ""));
 headers.push(new Header("Content-Disposition", "attachment;filename=" + ""));

 if (headers != null && headers.length > 0)
 {
   for (var i = 0 ; i < headers.length;i++)
   {
    var header = headers[i];
    if (header.name == "Content-Disposition")
    {
     header.value="attachment;filename=myatt0.log"
    }
    if (header.name == "Content-Type")
    {
     var headerValue = header.value;
     try
     {
      var index1 = headerValue.indexOf('multipart/form-data');
      var index2 = headerValue.indexOf('boundary');
      if (index1 > -1)
      {
       tranByMulti = true;
       if (index2 > -1)
       {
        boundaryStr = headerValue.substring(index2+9);
        break;
       }
      }
     }
     catch(ex)
     {
     } 
    }
   }
  }  
  try {
      response = doHTTPRequest(httpAction, url, headers, binaryData, 20, 20, 20, null, true, false);
     }
     catch( cError ) {  
       }
  return response;

function basicAuth(httpHeader, username, password){
 var authHeader = new Header();
 authHeader.name = "Authorization";
 authHeader.value = "Basic " + lib.smis_CommonLib.encode64(username + ":" + password);
 
 httpHeader.push(authHeader);
 
 return httpHeader;
}