Retrieve the body for a Node.js HTTP Request

David
2 min readMar 1, 2023

When building Node.js applications, it is common to make HTTP requests to other servers. Most of the time, you need to access the body buffer object of the response returned by the server.

In this blog post, we will explore how to retrieve a body buffer object, using the native Node.JS http module.

First, let’s take a quick look at what a body buffer object is. When a server responds to an HTTP request, the response body is typically sent as a stream of bytes. Node.js processes this stream of bytes and converts it into a buffer object, which can be manipulated in memory. The buffer object contains the raw binary data of the response body.

The http module provides an http.request(…) method, which can be used to make HTTP requests to other servers. Similarly, to make https:// requests, the http module can be swapped for the https module.

Here’s an example of how to retrieve the body buffer object from within an HTTP request using a Promise:

const http = require('http');

const options = {
hostname: 'www.httpbin.org',
port: 80,
path: '/get',
method: 'GET'
};

const body = await new Promise((resolve, reject) => {
const req = http.request(options, (res) => {
const chunks = [];

res.on("data", (chunk) => {
chunks.push(chunk);
});

res.on("end", () => {
resolve(Buffer.concat(chunks));
});
});

req.on("error", (error) => {
reject(error);
});

req.end();
});

Let’s break down this code to understand how it works.

First, we require the http module. We then define our HTTP request options, which include the hostname, port, path, and HTTP method.

We then call the http.request method with our options object. This returns a http.ClientRequest object, which we can use to send the request to the server.

We define an array called chunks, which we will use to store the individual chunks of the response body as they arrive from the response stream. We then listen for the data event on the response object. This event is emitted whenever a new chunk of data is received from the server. We push each chunk into our ‘chunks’ array.

Finally, we listen for the end event on the response object. This event is emitted when the entire response has been received. At this point, we can concatenate all the individual chunks into a single buffer object using the Buffer.concat method and call resolve. This gives us the body buffer object, which we can then manipulate or process as needed.

In conclusion, making a request and retrieving a body buffer object from within an HTTP request in Node.js is a straightforward process using the built-in http module. By listening for the ‘data’ and ‘end’ events on the response object, we can easily concatenate the individual chunks of the response body into a single buffer object, which we can then manipulate as needed.

Typically, libraries such as fetch and axios can be used to simplify the process; but occasionally you might not be able to install or use them.

--

--

David

Coding is both my hobby and my job. I love writing about things I'm working on ❤️