Introduction
HTTP requests are a means to fetch data from a remote source. The data could be anything from an API, a website, or something else. Every developer should be familiar with how to perform HTTP requests using Node Js. In this article, we will be going through a popular way to extract data slight sent in the form of a JSON file, the GET HTTP request. Let’s dig deeper into the way we can use the HTTP request body to extract data. Request Body is where we can put any additional information that we have to send to the server. In the body of the request, we are free to place virtually whatever we want. The request body sends additional information to the server to fetch data from the request correctly.
GET HTTP request
The request body - the object passed in the connection callback is a stream. Therefore, we listen to the contents of the body in chunks.
Let’s go through a code sample to understand it better:
Also, check out GET v/s POST requests
codingninjas.js
const https = require("https");
const url = "https://jsonplaceholder.typicode.com/posts/1";
https.get(url, res => {
res.setEncoding("utf8");
let body = "";
res.on("data", data => {
body += data;
});
res.on("end", () => {
body = JSON.parse(body);
console.log(body);
});
});
To run the above code, use the command -
node codingninjas.js
OUTPUT

Now let’s go through the code and try to understand what is happening. The HTTP.get method expects the first argument as an URL, and the second argument is a callback. The return statement is an HTTP.ClientRequest Object.
We have a small idea of how these HTTP request body extracts the body data using the get module. The request body can be in the form of XML or JSON.
Let’s consider an Axios and JSON file,
const axios = require('axios')
axios.post('https://whatever.com/todos', {
todo: 'Buy the milk.'
})
For the above code, the server-side script using ExpressJs is:
const express = require('express')
const app = express()
app.use(
express.urlencoded({
extended: true
})
)
app.use(express.json())
app.post('/todos', (req, res) => {
console.log(req.body.todo)
})
But, what if we want to access the chunks of data we talked about in the above example.
We get the data by listening to the streams of data events. As soon as the data ends, the stream end event is called. Like we want the request body to give us the chunks of the data.
Still, it needs body more clarity, right?
Let’s consider a code sample:
const server = http.createServer((req, res) => {
// we can access HTTP headers
req.on('data', chunk => {
console.log(`Data chunk available: ${chunk}`)
})
req.on('end', () => {
//end of data
})
})
So we have already talked about the data and the end streams and using the above code, we can create a simple request to display the chunk of data that is available. Let’s consider the given case to understand the HTTP request body better.
If we have to access the data we assume that we will return a string, when we listen to the stream data, end the stream only then we parse the string to JSON.
const server = http.createServer((req, res) => {
let data = '';
req.on('data', chunk => {
data += chunk;
})
req.on('end', () => {
console.log(JSON.parse(data).todo); // 'Buy the milk'
res.end();
})
})