I want to use Google Apps Script
with a Google Doc
to send a PDF file uploaded to Google Drive
to an API that I am creating. The API summarizes some of the contents of the text of the PDF file and returns an HTML file containing the summary.
The API uses fast API and is working well with Swagger UI, but not when called from Google Apps Script
. Here is the GAS code:
function process_PDF_file() {
const PDF_fileId = "###"; // This is the file ID of the PDF uploaded to Google Drive
// that will be sent to the API
var url = "###"; This is the url of the API
const payload = {file: DriveApp.getFileById(PDF_fileId).getBlob() };
let config = { muteHttpExceptions: true, payload };
let response = UrlFetchApp.fetch(url, config);
var data = JSON.parse(response.getContentText());
Logger.log(data);
}
Here are routes of the API, which is written in Python:
@app.get("/")
async def root():
return {"message": "Hello Summary Creator"}
@app.post("/file")
async def endpoint(uploaded_file: bytes = File()):
content = uploaded_file
filename = "temp.pdf"
with open(filename, "wb") as f:
f.write(content)
main_routine()
return {"Summary": f"{summary_download}"}
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8000)
Running the GAS code results in the following entry in the Execution log:
10:56:22 AM Info {detail=[{type=missing, url=https://errors.pydantic.dev/2.9/v/missing, input=null, loc=[body, uploaded_file], msg=Field required}]}
A similar question was asked by user3347814 on Oct. 20, 2023 at 14:03: How to use Google App Script to send a file using POST request
The previous question, however, concerned multipart/form-data, which might not apply to my situation. In any event, I tried using using some techniques for handling multipart/form-data, including the GAS library supplied by Tanaike, who gave the accepted answer to the previous question, but I still received similar error messages when running the GAS code.
Any suggestions for fixing the problems would be appreciated.
Mike B. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
10