0

I can't get file uploads to work with FunctionFramework when using Express.

I've created an example repo: https://github.com/Boardtale/FunctionFrameworkAndUploadingFiles/blob/master/index.js.

I've tried using Multer, Formidable, Busboy, and even a native approach. All of them fail when using FunctionFramework but work perfectly fine when I call Express directly and listen on a port.

It seems like something is happening behind the scenes that I can't figure out. I’ve also tried adding different body parsers, but nothing helps.

So here's the results I get for specific libs.
Multer: req.file is undefined
Formiddable: does not enter form.parse
Native: does not enter req.on('end'
Busyboy:

Error parsing the file upload: Error: Unexpected end of form at Multipart._final (W:\workspace\playground\node_modules\busboy\lib\types\multipart.js:588:17) at prefinish (node:internal/streams/writable:914:14) at finishMaybe (node:internal/streams/writable:928:5) at Writable.end (node:internal/streams/writable:843:5) at onend (node:internal/streams/readable:946:10) at process.processTicksAndRejections (node:internal/process/task_queues:77:11) Exception from a finished function: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

Ofc file is sent with Content-Type: multipart/form-data; boundary=<calculated when request is sent>

7
  • Can you try to downgrade or reinstall dependencies and try? If you are still facing problem, please share error logs if you have any. Commented Sep 9, 2024 at 10:54
  • @RoopaM I get the error all the time. I even created this project from the scratch to seperate the issue and see if that's my project or it's just how it works. Turns out, it is how it's working. Sometimes I get errors, sometimes I do not as request is hanging. I put comments in specific libraries that do the error but I will update my question with errors that I get from those that do error Commented Sep 9, 2024 at 11:18
  • Can you have a look at [this article](https://dev.to/collegewap/fix-cannot-set-headers-after-they-are-sent-to-the-client-19gk )? Commented Sep 9, 2024 at 14:41
  • @RoopaM is this correct article? Article is about double sending response. The issue here is related to FunctionsFramework only. Express is working fine without FunctionsFramework but when hosted via FunctionsFramework (or real Google Cloud Functions) it's having problems with Multipart body Commented Sep 9, 2024 at 15:47
  • I apologize for irrelevant link, I hope the following threads might help you: thread1 & thread2. Commented Sep 10, 2024 at 11:03

1 Answer 1

1

As mentioned in the similar thread by @samthecodingman

Firebase Functions are constructed upon the functions-framework-nodejs package (or, alternatively, an in-house version of the same code), which parses the body before your code ever runs. This behavior is recorded in this document.

This indicates that the request's body stream has already been exhausted when you attempt to feed it to Busboy, which is why you are seeing the "Unexpected end of form" error message. You must use the rawBody property that the framework added to the request in order to access and execute the original request content.

You can also refer to this stackoverflow thread

Sign up to request clarification or add additional context in comments.

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.