How to access POST form fields in Express
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Express, POST form fields are usually read from req.body, but that only works after you add the right body-parsing middleware. The exact middleware depends on the request content type: URL-encoded forms, JSON bodies, and multipart form uploads are handled differently.
Standard HTML Forms Use express.urlencoded
Traditional browser forms usually submit as application/x-www-form-urlencoded. For that case, enable:
With a matching HTML form:
After submission, the fields are available on req.body.
JSON Requests Use express.json
If the client sends JSON instead of an HTML form:
This is common for frontend applications using fetch, Axios, or mobile clients.
The key idea is that Express does not magically parse every body format the same way. The middleware must match the content type being sent.
Multipart Forms Need Specialized Middleware
File uploads and multipart forms are different. express.urlencoded and express.json are not enough. A common solution is multer:
In that case:
- text fields are still available on
req.body - uploaded file metadata appears on
req.fileorreq.files
Middleware Order Matters
The parser must be registered before the route:
If the middleware is added after the route, req.body will be undefined or empty for that handler.
That ordering mistake is one of the most common causes of "why is req.body empty?"
Example With Validation
A small validation example makes the normal flow clearer:
This is the usual pattern: parse, destructure, validate, then use the fields.
If you are debugging an empty body, inspect the request's Content-Type header first. A form sent as JSON, multipart, or URL-encoded data needs matching middleware, and a mismatch is often the real reason the fields never appear.
It is also worth remembering that middleware order affects every downstream route. If you split your Express app into routers, make sure the body parser is applied at the app level or mounted before the router that needs req.body.
For security-sensitive forms, validate and sanitize the fields after parsing instead of assuming middleware parsing means the values are trustworthy. Parsing only turns bytes into JavaScript objects; it does not guarantee that the content is safe or complete.
That matters in production.
Common Pitfalls
One common mistake is trying to read req.body without any body-parsing middleware.
Another issue is using express.urlencoded for JSON requests or express.json for classic form posts and expecting both to behave the same.
A third problem is forgetting that multipart form uploads need different middleware such as multer.
Finally, developers often place the parser middleware after the route declarations and then spend time debugging an empty req.body.
Summary
- POST form fields in Express are usually read from
req.body. - Use
express.urlencodedfor standard HTML form posts. - Use
express.jsonfor JSON request bodies. - Use multipart middleware such as
multerfor file-upload forms. - Make sure the parser middleware is registered before the route handlers that need it.
- Match the parser to the incoming content type before reading form fields.
Related reading
- How to access Session variables and set them in javascript?
- How to access the first property of a Javascript object?
- How to add 30 minutes to a JavaScript Date object?
- How to add new elements to an array?
- How to align a <div> to the middle (horizontally/width) of the page
- How to align content of a div to the bottom
- How to align text input correctly in react native?
- How to allow only numeric (0-9) in HTML inputbox using jQuery?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.