GET HTTP Request Body Data using Node Js

Rubleen Kaur
Last Updated: May 13, 2022

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 is 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:

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

 

Frequently Asked Questions

 

  1. What are different kinds of HTTP requests?

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


     
  2. Can HTTP request GET have a body?

    Ans: 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 on parsing are separate from the criteria on method semantics.

     
  3. What is the body of the HTTP GET requests ?

    Ans:  The Request Body can put any additional information that we have 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.

 

Key Takeaways

 

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 bodythe 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. 

 

Happy Learning Ninjas!

 

Was this article helpful ?
0 upvotes