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

This is the payload and there are literally boundaries...

------WebKitFormBoundaryuAR8WB8C9wPIJ8V9
Content-Disposition: form-data; name="attributes"

{
  "name": "Photo.pdf",
  "parent": {
    "id": "0"
  },
  "content_created_at": "2012-12-12T10:53:43-08:00",
  "content_modified_at": "2012-12-12T10:53:43-08:00"
}
------WebKitFormBoundaryuAR8WB8C9wPIJ8V9
Content-Disposition: form-data; name="file"; filename="LR10440458.pdf"
Content-Type: application/pdf


------WebKitFormBoundaryuAR8WB8C9wPIJ8V9--

 

I tried it with cURL and it worked just fine. But this is not what I want...

curl --location --request POST 'https://upload.box.com/api/2.0/files/content' \
--header 'Content-Type: multipart/form-data' \
--header 'Authorization: Bearer <dev token>' \
--form 'attributes="{
  \"name\": \"schedule.png\",
  \"parent\": {
    \"id\": \"0\"           
  }
}"' \
--form 'file=@"/Users/<name>/Downloads/image(2).png"'

 

I was using postman on the browser but I changed to using postman app then it worked just fine.

Amane1
Giga Guru

I have found a related topic in the community.
Any idea of how to upload a binary file attachment from ServiceNow to Box with POST? 

 

But unfortunately, there is no solution. The link they provided is something wrong.