How to access Raw Body of a Post Request in Express.js ?
Last Updated :
02 Apr, 2024
Raw Body of a POST request refers to unprocessed or uninterpreted data sent in the request before Express or any middleware processes or understands it. It's like raw ingredients before the cooking begins.
In this article we will see various approaches to access raw body of a post request in Express.js.
Steps to Create Application:
1) Initialize a New Node.js Project:
Open your terminal or command prompt and navigate to the desired directory for your project. Then, run the following command to create a new Node.js project:
npm init -y
2) Install Express.js:
npm install express
3) Set Up the Express Application
To establish the entry point for your Express.js application, begin by creating a new JavaScript file in your project directory naming it for example app.js. In this file, import the Express.js module
const express = require('express');
const app = express();
Project Structure:

Dependencies in package.json:
"dependencies": {
"express": "^4.18.3"
}
Let's explore practical examples that illustrate each of the three approaches for accessing the raw body of a POST request in Express.js.
Note: To obtain the desired output, open postman and send a POST request. Populate the request body with a simple text, then proceed to send the POST request. This action should result in the expected output.
Using express.raw() Middleware
In modern versions of Express (4.16.0 and higher), developers can utilize the express.raw() middleware to seamlessly access the raw body of a POST request. Notably, this middleware efficiently parses the request body as a Buffer, offering a straightforward and effective method for handling raw data in your Express.js applications.
JavaScript
const express = require('express');
const app = express();
// Set the encoding for express.raw()
app.use(express.raw({ type: '*/*', limit: '10mb' }));
app.post('/modern', (req, res) => {
const rawBody = req.body;
console.log('Modern Approach:', rawBody.toString('utf-8'));
res.send(`How to access raw body of a POST request
in Express.js? Modern Approach Received: `
+ rawBody.toString('utf-8'));
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});
Output:

Using body-parser Middleware
In the earlier days of Express.js, developers commonly relied on the body-parser middleware as the primary tool for parsing raw request bodies. Utilizing body-parser.text() was the standard practice to manage the raw data efficiently. However, it's essential to note that this approach has become outdated with the evolution of Express.js.
Install body-parser module before implementing this approach:
npm install body-parser
JavaScript
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.text());
app.post('/deprecated', (req, res) => {
const rawBody = req.body;
console.log('Deprecated Approach:', rawBody);
res.send(`How to access raw body of a POST
request in Express.js? Deprecated Approach Received: `
+ rawBody);
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});
Output:

Using data and end Events
For those who prefer a more hands-on approach, manual handling of the data and end events offers a level of control over the raw body. This method involves accumulating chunks of data as they arrive and processing them when the request ends. Here's a glimpse into how this approach can be implemented:
JavaScript
const express = require('express');
const app = express();
// Manual Handling: Using data and end events
app.post('/manual', (req, res) => {
let rawBody = '';
req.on('data', (chunk) => {
rawBody += chunk;
});
req.on('end', () => {
console.log('Manual Handling:', rawBody);
res.send(`How to access raw body of a
POST request in Express.js? Manual Handling Received: `
+ rawBody);
});
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});
Output:

Similar Reads
How To Make A GET Request using Postman and Express JS
Postman is an API(application programming interface) development tool that helps to build, test and modify APIs. In this tutorial, we will see how To Make A GET Request using Postman and Express JS PrerequisitesNode JSExpress JSPostmanTable of Content What is GET Request?Steps to make a GET Request
3 min read
How to Handle a Post Request in Next.js?
NextJS is a React framework that is used to build full-stack web applications. It is used both for front-end as well as back-end. It comes with a powerful set of features to simplify the development of React applications. In this article, we will learn about How to handle a post request in NextJS. A
2 min read
How to Access Request Parameters in Postman?
Request parameters are additional pieces of information sent along with a URL to a server. They provide specific details about the request, influencing the server's response. Parameters typically follow a 'Key=Value' format and are added to the URL in different ways depending on their type.Table of
4 min read
How to access POST form fields in Express JS?
Express JS is used to build the application based on Node JS can be used to access the POST form fields. When the client submits the form with the HTTP POST method, Express JS provides the feature to process the form data. By this, we can retrieve the user input within the application and provide a
3 min read
How to insert request body into a MySQL database using Express js
If you trying to make an API with MySQL and Express JS to insert some data into the database, your search comes to an end. In this article, you are going to explore - how you can insert the request data into MySQL database with a simple Express JS app.Table of Content What is Express JS?What is MySQ
3 min read
How to resolve req.body is empty in posts error in Express?
In Express the req.body is empty error poses a critical challenge in web development, particularly in the context of processing POST requests on the server side. This issue arises when the server encounters difficulties parsing the request body, resulting in an empty or undefined req.body object. De
4 min read
How to receive post parameter in Express.js ?
Express is a small framework that sits on top of Node.jsâs web server functionality to simplify its APIs and add helpful new features. It makes it easier to organize your applicationâs functionality with middleware and routing; it adds helpful utilities to Node.jsâs HTTP objects; it facilitates the
3 min read
How to Access Dynamic Variable {{$guid}} inside Pre request in Postman?
During the testing stage in API development, it is important to extensively test the APIs to ensure they work without any problems in the production stage. We need a huge amount of test data for this purpose. Generating it manually can be a cumbersome and time-consuming task. Instead, we need some k
4 min read
How do you access query parameters in an Express JS route handler?
Express JS is a popular web framework for Node JS, simplifies the development of web applications. One common task is accessing query parameters from URLs within route handlers. In this article, we'll explore the fundamentals of working with query parameters in Express JS and demonstrate how to acce
2 min read
How to set header request in Postman?
Postman is a powerful API development tool that offers a feature known as environment variables. These variables are used for efficient testing and development of APIs by allowing users to manage dynamic values across requests easily. In this article, we will learn How you can set header requests in
2 min read