Using box API [Upload file] from Servicenow

Amane1
Giga Guru

Hi Community.

I am trying to connect to box from ServiceNow using background script.

Here is my code.

 

var table_name = "x_common_center";
var sys_id = "01da168d834d5250e3d896b6feaad333";

var attachment = new GlideSysAttachment();
var agr = attachment.getAttachments(table_name, sys_id); // create attachment GlideRecord

while (agr.next()) {
    // for each attachment on the incident record
    var file_name = agr.getValue('file_name');
    var content_type = agr.getValue('content_type');
    gs.info('File Name: ' + file_name);
    gs.info('Content Type: ' + content_type);

    // Get the content stream (binary data)
    var is = attachment.getContentStream(agr.getUniqueValue());
    if (!is) {
        gs.error('No file content found for attachment: ' + file_name);
        continue; // Skip if no file content is found
    }

    // Define boundary and metadata
    var folder_id = "287704293536"; // Replace with actual folder ID in Box
    var date = new GlideDateTime();
    var formattedDate = date.getValue().replace(" ", "T") + "Z"; // Convert to RFC 3339 format
    var boundary = '----WebKitFormBoundary' + date.getNumericValue(); // Define unique boundary
    gs.info("Boundary: " + boundary);

    var request = new sn_ws.RESTMessageV2('global.Box Test', 'file upload to box');
    request.setHttpMethod('POST'); 
    request.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + boundary);

    // Use getStreamWriter() to add boundaries and parameters
    var writer = request.getStreamWriter();

    // Write attributes part (metadata)
    writer.writeString("--" + boundary + "\r\n");
    writer.writeString('Content-Disposition: form-data; name="attributes"\r\n\r\n');
    writer.writeString('{"name":"' + file_name + '","parent":{"id":"' + folder_id + '"}, "content_created_at": "' + formattedDate + '", "content_modified_at": "' + formattedDate + '"}\r\n');

    // Write file part (binary content)
    writer.writeString("--" + boundary + "\r\n");
    writer.writeString('Content-Disposition: form-data; name="file"; filename="' + file_name + '"\r\n');
    writer.writeString('Content-Type: ' + content_type + '\r\n\r\n');
    writer.writeStream(is); // Write binary content (file)
    writer.writeString("\r\n--" + boundary + "--\r\n");

    // Execute the request
    var response = request.execute();
    var response_body = response.getBody();
    var response_code = response.getStatusCode();

    // Log the response
    gs.info("Response Code: " + response_code);
    gs.info("Response Body: " + response_body);
}

 

According to this source
it should work fine but response always returns: {"message": "API upload did not contain a file part","type":"error"}

 

Is there any other way to set binary data to reqeuest body?

I would be grateful if anyone has used the Box file upload API before. I have searched every single website I could find, but I still haven't figured out how.
 

13 REPLIES 13

What the log you are getting for Content-type?

The first script:

while (agr.next()) {
    // for each attachment on the incident record
    var file_name = agr.getValue('file_name');
    var content_type = agr.getValue('content_type');
    gs.info('File Name: ' + file_name);
    gs.info('Content Type: ' + content_type); // application/msword

 

 

I think application/msword is the problem as that is not what your code is adding content-type in the rest call

It should be "multipart/form-data" as mentioned in the reference. But I changed the value to [application/msword] just in case but the first script returns the same error as before: {"code":"bad_request","help_url":"http://developers.box.com/docs/#errors","status":400,"message":"API upload did not contain a file part","type":"error"}

 

and the second script succeeds but its content is corrupted.

 

Screenshot 2024-10-16 at 3.26.05 PM.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

image (1).pngimage(2).png

Amane1
Giga Guru

Just in case, I have tried it in postman as well but the result is:

"the request was rejected because no multipart boundary was found"

Screenshot 2024-10-17 at 6.25.36 PM.png

I have read websites that suggest sending API without defining "content-type" as the browser sets it automatically but still didn't work and the response code turned 401 unauthorized.

Screenshot 2024-10-17 at 6.28.26 PM.png