title: NodeJS - Edit and proxy a multipart/form-data request
body: <p>I have a microservice that proxies every request adding one more field to it. With normal requests it's very easy, just add the field in the <em>request.body</em> and properly set the headers but for multipart/form-data requests I'm in trouble since days because if I add a field in the <em>request.body</em>, it will disappear.</p>
  
  <pre class="lang-js prettyprint-override"><code>const router = express()
  const routes = require('~/routes')
  const passport = require('passport')
  const proxy = require('http-proxy-middleware')
  
  router.use(passport.initialize())
  require('./modules/passport-jwt')(passport)
  
  router.use('/', routes)
  
  router.use(
      '/account',
      passport.authenticate('jwt', { session: false }),
      proxy({
          target: process.env.ACCOUNT_SERVICE,
          pathRewrite: { '/account': '/' },
          onProxyReq: restream
      })
  )
  
  const restream = async function (proxyReq, req, res, options) {
      if (req.user) {
          if (
              req.headers['content-type'] &amp;&amp;
              req.headers['content-type'].match(/^multipart\/form-data/)
          ) {
              req.body.reqUser = req.user
          } else {
              const requestBody = JSON.stringify({ ...req.body, reqUser: req.user })
              proxyReq.setHeader('Content-Type', 'application/json')
              proxyReq.setHeader('Content-Length', Buffer.byteLength(requestBody))
              proxyReq.write(requestBody)
          }
      }
  }
  </code></pre>
  
  <p>When the request arrives to the other microservice, the <em>request.body</em> is empty and after it will be written by <a href="https://github.com/expressjs/multer" rel="nofollow noreferrer">multer</a> that will put multipart/form-data params into the <em>request.body</em>.</p>
  
  <p>I really need a solution that let me append a field into a multipart/form-data request in the proxy <em>restream</em> function.</p>
  
  <p>I tried everything to succeed in this but I'm stuck. I hope everything is clear from my side. Don't be afraid to ask for more details if you need them. I'm begging for your help.</p>