0

When i am uploading file from frontend with formdata as body of post request, Backend is returning 400 bad request.

const formData = new FormData();  
formData.append("file", this.fileSelected);  
formData.append("type", "ALLOC");

when i check the backend logs, then its saying file is undefined, It means proxy drops the multipart formdata client --> proxy server --> backend global middlewares

app.use(morgan("dev"));
app.use(cors(corsOptions));
app.use(bodyParser.json());
app.use(express.urlencoded({ extended: false }));
// restream parsed body before proxying
let restream = function(proxyReq, req, res, options) {
    console.log("################");
    console.log(req.body);
    console.log(req.file);
    console.log(req.type);
    console.log(req.filename);
    if (req.body) {
        let bodyData = JSON.stringify(req.body);
        proxyReq.setHeader('Content-Type','application/json');
        proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
        proxyReq.write(bodyData);
    }
}

router.use('',checkAuthentication,createProxyMiddleware({
    target: config.MAIN_SERVER_BASE_URL,
    changeOrigin: true,
    on: {
        proxyReq: restream
    }
}));

I need to handle both json request and formdata.

2
  • Don't post code as screenshots, instead, post them as plain text with code format Commented Jul 2, 2024 at 3:44
  • Hi, for formData, Multer the middleware is required. Commented Jul 4, 2024 at 8:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.