1

I am using google form to upload files to google drive. Now I want to be able to upload these (uploaded)files from google drive to external api using google apps script. (This api of GoCD to be specific.)

I tried to upload the file this way which is probably wrong, but it doesn't work:

function main() {
  var url = "https://<my-server-url>/go/files/Pipeline1/1/Stage1/1/Job1/folder/";
  var form = {
    file : DriveApp.getFileById("<file-id>"),
  };
  uploadFile(url,form);
}

function uploadFile(url,form) {
  
  var options = {
    "method" : "POST",
    "header" : {
    "Confirm" : true
    },
    "muteHttpExceptions" : true,
    'zipFile' : form
  };
  
  var response = UrlFetchApp.fetch(url,options);
  Logger.log("Response body: " + response.getContentText());
}

Is there any other way to make POST api call to upload files from drive? Also, any help with resources for doPost() method of google apps script will be appreciated.

Community
  • 1
  • 1
Tech Girl
  • 169
  • 2
  • 17

2 Answers2

2

See my post and answer by @Tanaike, as I think the answer I found also answers your question, @Shivani-Shinde: How do you create and then insert an image file from Google Drive into a Google Apps Script UrlFetchApp.fetch call to an External API?.

var formData = {
  'upload': DriveApp.getFileById("###").getBlob()
};
Chris
  • 473
  • 5
  • 18
  • I have tried this code and it's working fine, but the file is saving as PDF. Do you know how to save the blob data as .docx? – stackflow Sep 01 '20 at 09:32
0

Answering this question, if anyone needs it.

So, I tried all the ways to upload files, but it won't work. Turns out you cannot directly get a file from drive and upload it to external api. You have to have a middleware (here, node app) where the file will be stored first locally and then upload it to api. I have used node app and Drive API V3 to download the file and then made a post call to external api.

Thanks!

Tech Girl
  • 169
  • 2
  • 17