Categories
JavaScript Answers

How to get raw request body using Express and Node.js?

Spread the love

Sometimes, we want to get raw request body using Express and Node.js.

In this article, we’ll look at how to get raw request body using Express and Node.js.

How to get raw request body using Express and Node.js?

To get raw request body using Express and Node.js, we can use the body-parser package.

For instance, we write

const bodyParser = require('body-parser');

app.use(bodyParser.raw(options));

app.get(path, (req, res) => {
  console.log(req.body)
});

to call bodyParser.raw to return a middleware that parses the request body into a buffer.

Then we call app.use with the returned middleware to use it our app.

Finally, in our GET request handler, we get the raw request body as a buffer object from req.body.

Conclusion

To get raw request body using Express and Node.js, we can use the body-parser package.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *