Form with POST method and enctype="multipart/form-data" returns empty object in Nodejs Express.
In app.js I have used:
const app = express()
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
While using only POST type and action in the <form> tag
req.body gives output in json smoothly and have no issues.
But using enctype="multipart/form-data" in express req.body returns { } - empty object
Can anyone help with this?
The
urlencodedmiddleware only handles theapplication/x-www-form-urlencodedcontent type andjsonhandles theapplication/jsoncontent type. If you specifically need to usemultipart/form-data(e.g. if you need to handle file uploads), you'll need a package for that, since as of writing, express doesn't come with a multipart parser out of the box. Common packages used to handle multipart aremulterandformidable.Alternatively, if you don't need to upload files or other binary content, just remove the
enctypeattribute as<form>defaults toapplication/x-www-form-urlencoded.