REST Attachment currupted file

jacobo
Tera Contributor

Hi All

 

I need to send an excel file to an external API with post method. I'm using  this code to send it I have a successful result , the excel file is attached in the client environment (it is not servicenow)
When I try to download the attachemtent , excel can't open it beacuse the file is corrupted

 

Do you know what could be the possible error on my script?

 

Regards

 

var attachment = new GlideSysAttachment();
 
var agr = attachment.getAttachments('sc_req_item', 'ab480111c368ea9cda547fe4e4013143');
 
while(agr.next())
gs.info(agr.getValue('file_name'));
gs.info(agr.getValue('sys_id'));
 
var Attachname = agr.getValue('file_name');
var attachmentSysId = agr.getValue('sys_id');
 
var attach=""    
var is = new GlideSysAttachment().getContentStream(attachmentSysId);
var reader = new GlideTextReader(is);
var attachmentData = ' ';
while((attachmentData = reader.readLine()) != null) {
    // gs.log(attachmentData)
attach += attachmentData+"\r\n"
}
    // Define a boundary for the multipart/form-data request
    var boundary = '----WebKitFormBoundary7MA4YWxkTrZu0gW';
    
    // Build the multipart body string
    var body = '';
    // File part: adjust the "name" and "filename" as needed
    body += '--' + boundary + '\r\n';
    body += 'Content-Disposition: form-data; name="files"; filename="'+Attachname+'"\r\n';
    body += 'Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\r\n\r\n';
    // Append the file content. Note: if you need to send Base64 instead, retrieve and append Base64 content.
    body += attach;
    body += '\r\n';
    // End boundary
    body += '--' + boundary + '--\r\n';
 
    // Create and configure the REST message
    var restMessage = new sn_ws.RESTMessageV2();
    restMessage.setHttpMethod('POST');
    restMessage.setLogLevel('all')
    restMessage.setEndpoint(External URL);
    restMessage.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
   gs.log(body)
    restMessage.setRequestBody(body);
 
    try {
        // Execute the REST call and log the response
        var response = restMessage.execute();
        var responseBody = response.getBody();
        gs.info('Response: ' + responseBody);
    } catch(e) {
        gs.error('Error sending Excel attachment: ' + e);
    }
 
7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@jacobo 

So it means the file data is not sent correctly.

How does the 3rd party expect the file data base64encoded data or content stream?

Did they share the sample JSON request body on how they expect the excel data?

there is a article explaining about using multi-part and providing the code: Script to send multipart (including text and binary) RestAPI

And also there is a ready-to-use-solution in the Share: MultiPart Upload Helper 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@jacobo 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

jacobo
Tera Contributor

Hi all,

 

I still have the same issue , I fund this KB article 

 

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0745010

 

Do you think it would be works?

 

Regards