I am trying to send a file to the REST API through Google Apps Script. The idea is that I have a process for creating copies of Google Doc, and I want you to be able to publish these newly created Docs in a third-party system.
I found in UrlFetchApp that I can send files. However, I am having trouble sending the correct header values.
My query looks like this:
var file = DriveApp.getFileById(fileId); var body = { "file": file.getAs(MimeType.PDF) }; var headers = { 'Content-Disposition': 'attachment; filename="'+ file.getName() +'"', 'Content-Length': file.getSize() };
My parameters when calling UrlFetchApp.fetch (url, options) look like this:
({ method:"POST", headers:{ 'Content-Disposition':"attachment; filename=\"My Merge Development_row_1.pdf\"", 'Content-Length':90665, Authorization:"Bearer TOKEN" }, contentType:"application/x-www-form-urlencoded", muteHttpExceptions:true, payload:{file:Blob} })
The API to which I send files requires a 'Content-Length' header. But when I try to set the value for the 'Content-Length' header, I get a Script error message, "Attribute provided with invalid value: Header:Content-Length" . If I do not set the Content-Length header, then the API responds that the Content-Length and file size do not match.
Any ideas on how I set the Content-Length header so that I can POST the file?
source share