Express.js - get the actual post request's body JSON

Viewed 3215

I am trying to get the actual request body JSON in express.js. But was not able to. All I found on the internet is how to get a body of type ReqBody, but I want the actual JSON or, at the more preferably, a way to query fields from ReqBody. (online shows body.username and body.password but what about any other field name? I cant just pass a field name foo and try to get body.foo.

Here is what I got so far:

router.post('/', function (req, res, next){
  let body = req.body;
//?????
});


1 Answers

If the content-type of the body of your Post is JSON, then you can do this:

router.post('/', express.json(), function (req, res, next){
    console.log(req.body);
    // use req.body and send a response here
});

More commonly, you would configure express.json() at a higher level so that it works for all routes on that router or even put it on the app object so it works for all routers too:

router.use(express.json());

router.post('/', function (req, res, next){
    console.log(req.body);
    // use req.body and send a response here
});

The express.json() middleware will check for the content-type that represents JSON and, if it matches, it will read the body of the post from the incoming stream, parse the JSON it finds there and put the resulting Javascript object into req.body. Any field the client puts in the JSON it sent with the Post will be in req.body.

In case you didn't realize, the body of the POST request is NOT read from the incoming stream by Express by default. It's left sitting in the OS or nodejs network buffers. So, until some code reads the body from the incoming stream and parses it, there's NOTHING in req.body. Usually, you install middleware such as express.json(), express.text() or express.urlencoded() or some other custom middleware and each of those looks at the content-type and if it matches what they know how to parse, then they read the body from the stream, parse it and do something with it (like put it in req.body). Other middleware like multer can read one or more file uploads from the body. There are lots of types of middleware for lots of types of content-types.

If you really wanted the raw, unparsed body (which there is no need for if it's JSON), then you can use the middleware express.raw(). express.raw() is different in that it reads the body from the stream, but doesn't parse it so if you use that one, it will take any content-type and put the raw data as a Buffer object in req.body. Whatever you do, don't use that one generically first because it will read all bodies and the other middleware will never get anything. Usually, if you're using that one, you use it on a request handler-specific basis, not as generic middleware.

yes but a ReqBody is predefined, as it is not a raw JSON. this is why I am asking how to get the raw json

You are under a misconception about req.body for JSON that is parsed by the express.json() middleware. It will contain whatever properties that the incoming JSON contains. This is 100% determined by the client and what JSON it sends and is not predetermined by some class at all. If the client sends a property named foobar as in this JSON {'foobar':true} and you have the express.json() middleware running on that incoming request, then req.body.foobar will contained the value true.

As I explained above, you could use the middleware express.raw() to get you the RAW JSON into a Buffer object (where you would then have to parse it yourself), but that is not required as the express.json() middleware will do all that for you.


FYI, there does not appear to be any reference to a class named ReqBody in either the nodejs source code or the Express source code. It seems likely that someone was using that as a shortcut to refer to the req.body object and it is not some class that is used here.

Related