Upload Files using the Multipart Post Method with Google Script

This code snippet shows how you can use the use the multipart post method to upload a file from Google Drive to Box using the Box API and Google Script. The PDF file is already on Google Drive, it gets the blob of the file using the File_ID and uploads to a specific Box folder (FOLDER_ID).

// Written by Amit Agarwal www.labnol.org

function uploadFile() {
  var boundary = 'labnol';
  var blob = DriveApp.getFileById(GOOGLE_DRIVE_FILE_ID).getBlob();

  var attributes = '{"name":"abc.pdf", "parent":{"id":"FOLDER_ID"}}';

  var requestBody = Utilities.newBlob(
    '--' +
      boundary +
      '\r\n' +
      'Content-Disposition: form-data; name="attributes"\r\n\r\n' +
      attributes +
      '\r\n' +
      '--' +
      boundary +
      '\r\n' +
      'Content-Disposition: form-data; name="file"; filename="' +
      blob.getName() +
      '"\r\n' +
      'Content-Type: ' +
      blob.getContentType() +
      '\r\n\r\n'
  )
    .getBytes()
    .concat(blob.getBytes())
    .concat(Utilities.newBlob('\r\n--' + boundary + '--\r\n').getBytes());

  var options = {
    method: 'post',
    contentType: 'multipart/form-data; boundary=' + boundary,
    payload: requestBody,
    muteHttpExceptions: true,
    headers: { Authorization: 'Bearer ' + getBoxService_().getAccessToken() },
  };

  var request = UrlFetchApp.fetch('https://upload.box.com/api/2.0/files/content', options);

  Logger.log(request.getContentText());
}

Unlike Google Drive that allows multiple files of the same name, Box is more restrictive. It rejects files that have names longer than 255 characters or duplicate files with the same name.

The HTTP multipart request is commonly used to upload files and other data over to a HTTP Server. A “multipart/form-data” message contains a series of parts separated by boundaries. Each part should contain the “Content-Disposition” header whose value is “form-data” and if a file is being sent to the server, the contentType should also be included.

If the same request is made with curl, the request will be:

curl https://upload.box.com/api/2.0/files/content \\
  -H "Authorization: Bearer ACCESS_TOKEN" -X POST \\
  -F attributes='{"name":"file.pdf", "parent":{"id":"FOLDER_ID"}}' \\
  -F file=@file.pdf

Amit Agarwal is a web geek, solo entrepreneur and loves making things on the Internet. Google recently awarded him the Google Developer Expert and Google Cloud Champion title for his work on Google Workspace and Google Apps Script.

Awards & Recognition

Google Developer Expert

Google Developer Expert

Google awarded us the Developer Expert title recogizing our work in Workspace

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional title for 5 years in a row

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator award for technical expertise

Want to stay up to date?
Sign up for our email newsletter.

We will never send any spam emails. Promise 🫶🏻