2

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.

2 Answers 2

3

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()
};
Sign up to request clarification or add additional context in comments.

1 Comment

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?
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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.