Read form-data on server Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Read form-data on server without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am using multer to upload images to server. To do this, I send a form-data request from the client. It became necessary to check if the user is authorized. To do this, I send an object with a username and password. But there was a problem: I just can’t get this data.

I use body-parser to get the request body.

I usually get the request body like this:

const reqBody = JSON.parse(JSON.stringify(req.body));

With any other request, everything is fine, but with form-data reqBody === {}. What to do?

Route using multer:'/image_for_profile',
    async(req, res) => {
        try {
            const reqBody = JSON.parse(JSON.stringify(req.body));
            console.log(reqBody) // {}

            let status;

            upload(req, res, err => {
                let error = '';

                if (err) {
                    if (err.code === 'LIMIT_FILE_SIZE') {
                        error = 'Размер изображения не должен превышать 2мб';
                    if (err.code === 'EXTENTION') {
                        error = 'Файл не является ни jpg, ни jpeg, ни png';
                    status = error;

            return res.status(200).json({
                msg: status || 'OK',
        } catch (err) {
            return res.status(500).json({
                msg: config.get('msgs.statuses.500err')

The request:

enter image description here


The problem is that you’re currently trying to access req.body before your form-data-parser has populated it. You need to move this into the upload-callback, multer will then have populated all text-based form-fields under req.body and there under the name of your form-field (data in your case).

// ...
 upload(req, res, err => {
      console.log(; // will be populated here
      // ...
We are here to answer your question about Read form-data on server - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji