req.body
The parsed request body, populated by body-parsing middleware.
Syntax
req.body Returns
object | undefined — The parsed body, or undefined if no parser ran.
Examples
app.use(express.json());
app.post('/echo', (req, res) => {
res.json(req.body);
});
Output
$ curl -X POST -d '{"a":1}' -H 'Content-Type: application/json' localhost:3000/echo
{"a":1}
app.use(express.urlencoded({ extended: true }));
app.post('/form', (req, res) => {
res.send(`hello ${req.body.name}`);
});
Output
$ curl -X POST -d 'name=Ada' localhost:3000/form
hello Ada
Notes
`req.body` is `undefined` until a body parser runs - add
`express.json()` and/or `express.urlencoded()` before the route. The
parser only acts when the `Content-Type` matches. Always validate
and size-limit body input.