-2

I created a script to upload files from local to Google Drive using post method, I tried to upload using curl but it doesn't work and I get the error Could not decode string.

Google Apps Script code:

function doPost(e) {
  var folder, folders = DriveApp.getFoldersByName("Uploads");
  if (folders.hasNext()) {
    folder = folders.next();
  } else {
    folder = DriveApp.createFolder("Uploads");
  }
  var data = Utilities.base64Decode(e.parameters.data);
  var blob = Utilities.newBlob(data, e.parameters.mimetype, e.parameters.filename);
  var id = "Your File Download Link: https://drive.google.com/uc?id="+folder.createFile(blob).getId();
  return ContentService.createTextOutput(id)
}

My curl command:

curl -i -X POST -H 'Content-Type: multipart/form-data' -F 'data=test.png' https://script.google.com/macros/s/AKfycbwcGij0UNl8c9OJEpHbUvGF4KTJYbBOkDA9m882BLJVnWpFrc4/exec
Javad
  • 373
  • 4
  • 20
  • 1
    Does this answer your question? [How to upload a file via POST (doPost) to a Google Script's Web App?](https://stackoverflow.com/questions/42217052/how-to-upload-a-file-via-post-dopost-to-a-google-scripts-web-app) – ale13 Jul 15 '20 at 13:36
  • @ale13 no i don't problem using html upload and work , but i want using curl for upload via post method. – Javad Jul 15 '20 at 14:35
  • Why do you want to use `doPost`? Have you already read https://developers.google.com/apps-script/guides/triggers? – Rubén Jul 15 '20 at 21:14
  • @Rubén Although this is my guess, from OP's curl command which doesn't use the access token, I thought that OP might want to upload a file without authorization. – Tanaike Jul 16 '20 at 06:55
  • Can you explain exactly what are you trying to achieve then? Moreover, does the answer posted solve your issue? @Javad – ale13 Jul 16 '20 at 12:56

1 Answers1

1

I think that in your case, it is required to modify both the Google Apps Script and the curl command.

Modification points:

  • In the current stage, the Web Apps cannot use multipart/form-data.
  • Unfortunately, when your curl command is used, no file is uploaded.
  • In this case, it is required to convert the file to base64 data and upload it as the string.
  • e.parameters is the array. In this case, please use e.parameter.

When above points are reflected to your script and curl command, it becomes as follows.

Modified script:

function doPost(e) {
  var folder, folders = DriveApp.getFoldersByName("Uploads");
  if (folders.hasNext()) {
    folder = folders.next();
  } else {
    folder = DriveApp.createFolder("Uploads");
  }
  var data = Utilities.base64Decode(e.postData.contents.split("data=")[1]);
  var blob = Utilities.newBlob(data, e.parameter.mimetype, e.parameter.filename);
  var id = "Your File Download Link: https://drive.google.com/uc?id="+folder.createFile(blob).getId();
  return ContentService.createTextOutput(id);
}

Modified curl command:

base64 test.png | curl -L --data-raw "data=$(</dev/stdin)" "https://script.google.com/macros/s/###/exec?filename=test.png&mimetype=image/png"
  • Please replace the URL of https://script.google.com/macros/s/###/exec with yours.

Note:

  • When you modified the script of Web Apps, please redeploy the Web Apps as new version. By this, the latest script is reflected to the Web Apps. Please be careful this.
  • In this case, as the test situation, please set the Web Apps as follows.
    • Execute the app as: Me
    • Who has access to the app: Anyone, even anonymous

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • 1
    for upload anyfile which mime is good? i mean `imetype=` – Javad Jul 20 '20 at 06:44
  • @Javad Thank you for replying. I have to apologize for the inconvenience. In this case, `imetype=` is `mimetype=`. This is my copy and paste mistake. So I modified it. And, if my proposed method was not useful for your situation, I apologize. – Tanaike Jul 20 '20 at 07:04
  • 1
    @Javad Is there anything that I can do for your question? If my answer was not useful for your situation. I have to apologize and modify it. If you can cooperate to resolve your issue, I'm glad. I would like to think of about the solution. – Tanaike Jul 23 '20 at 07:08