1

I checked this question but for some reasons the solution given is not working for me. In my express.js I have:

...

bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({
   extended: true
}));
app.use(bodyParser.json());
...

Run test using POSTman

enter image description here

app.post('/upload', function(req, res) {
   console.log(req.body) // undefined
   console.log(req.params) // undefined
})

and the result:

enter image description here

So both body & params are empty. Any suggestions?

Community
  • 1
  • 1
luiquao
  • 1,094
  • 4
  • 21
  • 46

1 Answers1

0

The reason the solution in the link you provided doesn't work is because that version of body-parser is out of date and doesn't include form-data parsing anymore (and it used to be bundled with express).

With that being said, based on your screenshot, it looks like you are sending data of type multipart/form-data(you can check this in the request headers) to your server and your code sample only shows middleware that handles urlencoded and json data types.

You need to add middleware that handles that data type. The latest body parser says (https://github.com/expressjs/body-parser):

This does not handle multipart bodies, due to their complex and typically large nature. For multipart bodies, you may be interested in the following modules:

So check out one of the above parsers. I use busboy and it works great.

mattr
  • 5,458
  • 2
  • 27
  • 22