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 request.body 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 request.body, it will disappear.
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'] &&
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)
}
}
}
When the request arrives to the other microservice, the request.body is empty and after it will be written by multer that will put multipart/form-data params into the request.body.
I really need a solution that let me append a field into a multipart/form-data request in the proxy restream function.
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.