POSTing to Function as multipart/form-data

3,105 views
Skip to first unread message

Rui Teixeira

unread,
Jan 16, 2018, 5:47:20 PM1/16/18
to Firebase Google Group
I've been trying to use Express its multer middleware to parse a request body POSTed as multipart/form-data in a function, with no success so far.

I've tested this on a local server and it works perfectly fine but it fails miserably when run via a Firebase Function. I've sought help/support in the official slack channel but no luck.

Here's the code:

const functions = require('firebase-functions');
const express = require('express');
const Multer = require('multer');

const app = express();
const multer = Multer();

const handleFieldsWithMulter = multer.fields([
  { name: 'a_field' },
  { name: 'another_field' },
  { name: 'yet_another_field' }
]);

const handlePostWithMulter = (req, res) => {
  const formData = req.body;
  res.status(200).send(formData);
}

const loggingMiddleware = (req, res, next) => {
  console.log(`request body: ${req.body}`);
  next();
}

app.post('/', loggingMiddleware, handleFieldsWithMulter, handlePostWithMulter);

const exchange = functions.https.onRequest(app)
module.exports = {
  exchange
}


I honestly can't find anything wrong with this. What's the issue here? I can see the data coming in, in the request body, but somehow Functions manages to screw everything up halfway through.


cURL:

curl -X POST \
  -F 'a_field=test' \
  -F 'another_field=stuff' \
  -F 'yet_another_field=things'


Data:

request body: --------------------------a172e3b547372a6b
Content-Disposition: form-data; name="a_field"

test
--------------------------a172e3b547372a6b
Content-Disposition: form-data; name="another_field"

stuff
--------------------------a172e3b547372a6b
Content-Disposition: form-data; name="yet_another_field"

things
--------------------------a172e3b547372a6b--

Expected response (works fine outside of the function):

{"a_field":"test","another_field":"stuff","yet_another_field":"things"}



Kato Richardson

unread,
Jan 17, 2018, 11:40:07 AM1/17/18
to Firebase Google Group
Hi Rui,

For starters, this code isn't valid JS syntax and would fail with a compile error:

module.exports = {
  exchange
}

Also, overriding module.exports has side effects on how the data is accessed. You should follow similar syntax to the guide and just attach your methods to exports instead of replacing them.

I.e. like this:

`exports.exchange = exchange;`

That probably won't help you resolve the underlying issue with "Functions screwing everything up" (a bit harsh of an assumption btw), but it's a good starting point to get the example accurate and functional. 

My initial guess would be that you are being affected by this bit from the Cloud docs on HTTP functions:

Cloud Functions provides request and response objects that are compatible with ExpressJS to make consuming HTTP requests simple. Cloud Functions automatically reads the request body, so you will always receive the body of a request independent of the content type. This means that HTTP requests should be considered to have been fully read by the time your code is executed. The nesting of ExpressJS apps should be used with this caveat—specifically, middleware that expects the body of a request to be unread may not behave as expected.

Since the body is already parsed, you may need a tool that parses the body after receipt instead of trying to intercept the body in middleware. I'm not 100% certain on this, but I think I'm reading that correctly.

There are examples handling multipart and files this way using busboy which may work better.

☼, Kato

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/88848781-44eb-4aeb-8e00-16df8e38486c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Doug Stevenson

unread,
Jan 17, 2018, 4:29:56 PM1/17/18
to Firebase Google Group
Also see these issues on Stack Overflow.  Cloud Functions does some pre-processing of certain kinds of requests (as Kato cited from the Cloud Functions docs) and it will break the expectations of some Express middleware.

To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.

To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/88848781-44eb-4aeb-8e00-16df8e38486c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

justin...@first-rung.com

unread,
Jan 17, 2018, 9:47:33 PM1/17/18
to Firebase Google Group
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.

To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/88848781-44eb-4aeb-8e00-16df8e38486c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages