Search code examples
google-apps-scriptgoogle-drive-api

How to receive --data-binary from post request in google drive app script


Here is my request

    curl --location --request POST 'https://script.google.com/macros/s/{My Web app url}/exec' \
--header 'Content-Type:  application/octet-stream' \
--header 'fileName: MyFileName' \
--header 'mimeType: application/octet-stream' \
--data-binary '@/Location/To/My/Local/File'

I am trying to receive this request in my google drive app script

function doPost(e) {
  var data = Utilities.base64Decode(e.postData.contents); <---- having issue with this line
  var blob = Utilities.newBlob(data, e.postData.type, "TempFile");
  var fileId = DriveApp.createFile(blob);
  
  return ContentService.createTextOutput("fileName:"+fileName + " fileId:" + fileId);
}

The file is binary, not image not zip, but other type of binary

What do I need to write to make it work ?


Solution

  • Modification points:

    • At Web Apps, the string value is retrieved from the event object e of doPost(e). In this case, when your curl command is used, I thought that the binary data might not be able to be correctly sent due to the issue of the character code. I'm worry about this.
    • And, when I saw your script of Web Apps, you are trying to decode the base64 data. But the curl command is not sent the file data as the base64.
    • In your script, it seems that fileName is not declared.
    • At Web Apps, in the current stage, unfortunately, the request header cannot be retrieved at the Web Apps side. It seems that this is the current specification.

    When above points are reflected to your curl command and script, those become as follows.

    Modified curl command:

    In order to send the file data as the base64 data, how about the following modified curl command?

    curl --location 'https://script.google.com/macros/s/{My Web app url}/exec' \
    --header 'Content-Type: application/octet-stream' \
    --data '{"file":"'"$(base64 /Location/To/My/Local/File)"'","filename":"sampleFilename"}'
    
    • By this curl command, the file is converted to the base64 data and sent to Web Apps.

    Modified Google Apps Script:

    In order to use the data from above curl command, how about the following modification?

    function doPost(e) {
      var contents = JSON.parse(e.postData.contents.replace(/\n/g, ""));
      var fileName = contents.filename;
      var data = Utilities.base64Decode(contents.file);
      var blob = Utilities.newBlob(data, e.postData.type, fileName);
      var fileId = DriveApp.createFile(blob).getId();
      return ContentService.createTextOutput("fileName:" + fileName + " fileId:" + fileId);
    }
    

    Note:

    • In your script of Web Apps, fileName is not declared. So the filename is sent with the curl command.
    • When you modified the Google Apps Script, please modify the deployment as new version. By this, the modified script is reflected to Web Apps. Please be careful this.
    • You can see the detail of this at the report of "Redeploying Web Apps without Changing URL of Web Apps for new IDE".

    References: