Getting `Error: Unexpected end of form` with Busboy and Firebase Functions

50 Views Asked by At

I am trying to send a video file to a firebase function that'll then take that file and process it. However, I keep encountering the error Busboy error: Error: Unexpected end of form when running the function.

import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
const Busboy = require('busboy')

admin.initializeApp()

export const uploadVideo = functions.https.onRequest(async (req, res) => {
  try {
    const busboy = Busboy({ headers: req.headers })
    let blob: Buffer

    busboy.on('file', (fieldname: any, file: any, filename: any, encoding: any, mimetype: any) => {
      console.log('Print me!')
    })

    busboy.on('finish', () => {
      console.log('Upload complete')
      res.status(200).send('DONE!')
    })

    busboy.on('error', (error: any) => {
      console.error('Busboy error:', error)
      res.status(500).send(`Internal server error: ${error}`)
    })

    req.pipe(busboy)
  } catch (e) {
    console.log('Exception:', e)
    res.status(500).send(`Internal server error: ${e}. ${JSON.stringify(req.headers)}`)
  }
})

What I've tried:

  • Content-Type is set by Postman automatically as multipart/form-data; boundary=<calculated when request is sent> when uploading a .webm video file. Which evaluates to something like content-type":"multipart/form-data; boundary=--------------------------117248356805800449660869
  • using busboy.end(req.rawBody)
  • Printing console.log('Print me!') within the .on('file' ... listener, it never gets printed.

Body looks like this when received by the function: <Buffer 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 35 36 39 38 34 30 37 38 30 35 32 30 31 33 39 32 37 34 37 37 30 30 ... 118 more bytes>

Here's the full error:

>  Busboy error: Error: Unexpected end of form
>      at Multipart._final (/Users/<user>/code/<project>/functions/node_modules/busboy/lib/types/multipart.js:588:17)
>      at callFinal (node:internal/streams/writable:698:12)
>      at prefinish (node:internal/streams/writable:710:7)
>      at finishMaybe (node:internal/streams/writable:720:5)
>      at Writable.end (node:internal/streams/writable:634:5)
>      at onend (node:internal/streams/readable:705:10)
>      at process.processTicksAndRejections (node:internal/process/task_queues:77:11)
0

There are 0 best solutions below