Search code examples
javascriptnode.jsmultipartform-datajava-serverbusboy

Sending form-data to Java server through a Node middleman


I have an application that uses axios to make requests to a node server which in turn makes requests to another java server.

Call to node server from client:

// here payload is FormData()
axios.post(url, payload).then((response) => {
    return callback(null, response);
}).catch((err) => {
    return callback(err, null);
});

In the node server, I listen to the request using busboy:

let rawData = '';
const busboy = new Busboy({headers: req.headers});

busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
    file.on('data', function (chunk) {
        rawData += chunk;
    });
});

Now the java server too expects FormData (just like the way I sent it to node). How do I get the FormData from node now? I have been googling hard and trying a lot of stuff in vain. Any solution not involving busboy will help too.


Solution

  • I had finally used the middleware busboy-body-parser which adds support for getting files from the request object as req.files. And once the file is there, I send it as form-data to the java web server using the form-data npm package. The req.files support used to be there by default in Express.js. But from 4.x, it has been deprecated.

    Multer is another really good middleware for handling multipart/form-data.