Table of contents
1.
Introduction
2.
GET HTTP request
3.
Frequently Asked Questions
3.1.
What are different kinds of HTTP requests?
3.2.
Can HTTP request GET have a body?
3.3.
What is the body of the HTTP GET requests?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

HTTP Request Body Data of Get using Node.js

Author Rubleen Kaur
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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);
  });
});
You can also try this code with Online Javascript Compiler
Run Code


To run the above code, use the command - 

node codingninjas.js

 

OUTPUT

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.'
})
You can also try this code with Online Javascript Compiler
Run Code

 

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)
})

 
You can also try this code with Online Javascript Compiler
Run Code

 

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
  })
})
You can also try this code with Online Javascript Compiler
Run Code

 

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();
  })
})
You can also try this code with Online Javascript Compiler
Run Code

 

Frequently Asked Questions

What are different kinds of HTTP requests?

The most frequent request methods are GET and POST, but many others, including HEAD, PUT, DELETE, CONNECT, and OPTIONS.

Can HTTP request GET have a body?

HTTP request messages can have a message body; parse messages with that in mind. Server semantics for GET is restricted, let’s say a body, has no semantic meaning to the request. The requirements for parsing are separate from the criteria on method semantics.

What is the body of the HTTP GET requests?

The Request Body can put any additional information that we have to send to the server. For the body of the request, we can place in virtual memory anything,be it the username and password of a person who is trying to log in to our system to the answers to a complex form of a survey. The body is quite essential because it represents, in many cases, the content per se that one wants to transmit.

Conclusion

Hey everyone, so let’s brief out the article. Let’s discuss in brief whatever we have discussed here. 

  •  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. This article has covered what is the body in the HTTP request GET module. The theme revolves around the request body's available chunk of data. When and how we can implement them by using easy code snippets. 
  • We have further covered that the Request Body 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. 
     

Isn’t Web Development engaging? Building new websites and using amazing animations and different APIs, don’t be scared if you cannot grasp the hold of this vast development. We have the perfect web development course for you to make you stand out from your fellow developers. 

Check out some of the amazing Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Basics of C, Basics of Java, etc. along with some Contests and Interview Experiences only on Coding Ninjas Studio.

Happy Learning Ninjas!

Live masterclass