简体   繁体   中英

Why request body is null in file upload with postman in Node.js

I am trying to upload file from postman to the Node.js server. I am using multer and body-parse to parse the body of the request.

Use case:

  1. when I use POST request with raw-JSON as Body in Postman, everything works fine and I can see the req.body exactly as provided in the Postman and also I can assign new key-value pair to the req.body so that these new variables can be use in further middlewares in the route.
  2. But when I try to upload a file from the postman with form-data as Body in Postman, then still I can get the value from req.body as provided. But after the multer middle layer, the newly assigned values to the req.body (from the middleware) shows corrupted like req.body becomes [Object: null prototype]. I can confirm that Multer is working fine but Multer is corrupting the req.body data (which is created in the route middleware itself)

Please help to identify the meaning of req.body=[Object: null prototype]

I ran into a similar issue. If you console.log(req.body) in the function. you will notice that the body is equal to [Object: null prototype] because Multer is handling the request as the form is multipart. So, if you console.log(req) object, you can find the values that can be retrieved and you can just do req.{Name_of_field} to get values.

If you need clarification please let me know, I might be able to share the code.

Shivam Mahajan wrote almost as it goes, but:

You can access text fields not in the req.{Name_of_field} , but req.body.{Name_of_field} , as said in multer docs .

you can use express busboy add these to your main page

const expressBusboy = require('express-busboy');

expressBusboy.extend(app);

and this will parse your form-data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM