req.body is empty prototype in onRequest firebase cloud functions (With multer)

Viewed 190

In my cloud function:

const app = express();
app.use(multer().array());
app.post("/", (req, res) => {
  console.log("hit", req.body.from);
  console.log("hit", req.body.from);
  return res.sendStatus(200);
});

const emailInboundWebhook = functions.https.onRequest(app);

module.exports = {
  emailInboundWebhook
}

I get this in logs:

i  functions: Beginning execution of "emailInboundWebhook"
>  hit undefined
>  hit undefined
i  functions: Finished "emailInboundWebhook" in ~1s

But when the same endpoint served as an express app(outside cloud functions):

const express = require("express");
const app = express();
const multer = require('multer');

app.get("/", async (req, res) => {
  res.status(200).json({foo: "Bar"});
})
app.use(multer().array());
app.post("/webhook", async (req, res) => {
  console.log("hit",req.body.to);
  console.log("hit",req.body.from);
  res.sendStatus(200);
});

app.listen(80, () => {
  console.log("App listening on 80");
})

Gives this:

hit a@some-email-inbound.some.url
hit Rahul Priyadarsi <myemailid@gmail.com>

The two results are for same email sent and these functions are fired as sendgrid webhooks which send multipart/form-data POST requests that contain details of the email sent to a@some-email-inbound.some.url from myemailid@gmail.com

I have no idea as to why the two results are different(I am testing them via ngrok and since the console log lines run, clearly the function is hit).

1 Answers

I had the same problem. Take a look in this issue: https://github.com/expressjs/multer/issues/572

Switch from Multer to Busboy, is also a good option.

One thing: In this situation use https.Request (from cloud-functions) instead of Request from express.

Related