Parsing the Request Body in http

Imagine your server as a busy mail-sorting facility. Letters (requests) arrive, and sometimes they carry extra notes inside (the request body). To understand that extra content, you need to carefully open the envelope and read what's inside. That's where parsing the request body in Node’s http package comes into play!

In this tutorial, you’ll discover how to read and handle the request body when it has a Content-Type of application/x-www-form-urlencoded. By the end, you’ll know how to transform an incoming stream of data packets into a JavaScript object you can use in your application.

Reading the Body of the Request

When your http server receives a request with data, it arrives as a “readable stream.” Think of a stream as a series of puzzle pieces that must be combined into one whole image—only when every piece is collected do you see the full picture.

With Node’s http.createServer, you can listen for the data event on the req object (the request envelope). Each time a “puzzle piece” of data arrives, that event fires, and you can tack the piece onto the larger string you’re building.

const http = require('http');

const server = http.createServer((req, res) => {
  let reqBody = '';

  req.on('data', (data) => {
    // 'data' is a chunk of the incoming request body
    // Keep appending to reqBody until all chunks have arrived
    reqBody += data;
  });
});

Once the entire “puzzle” arrives, the end event fires. This is your signal that you now have a complete request body, and you can examine or manipulate it as needed. It’s as if you’re saying, “We’ve finished receiving all puzzle pieces—time to see the final picture!”

const server = http.createServer((req, res) => {
  let reqBody = '';

  req.on('data', (data) => {
    reqBody += data;
  });

  req.on('end', () => {
    console.log(reqBody);
    // At this point, reqBody is the full string of the request body
  });
});

With these events, you have the raw string of your request body. But if the client sent form data, it’s likely in a particular format that you’ll need to parse before you can make sense of it. One common format is application/x-www-form-urlencoded.

Parsing application/x-www-form-urlencoded

Forms sent from HTML pages often arrive as one long string with keys and values glued together by = and &. For instance, a request might look like this:

name=Fido&color=black&age=1&description=Hello+World%21

In reality, this string represents multiple form fields. Each key and value can be broken down like puzzle pieces:

By the end, you might transform:

name=Fido&color=black&age=1&description=Hello+World%21

Into an object like:

{
  name: 'Fido',
  color: 'black',
  age: '1',
  description: 'Hello World!'
}

This approach is like translating a coded message into plain English. Once translated, you have a neat object you can use directly in your application—maybe storing it in a database, sending a response to confirm the form submission, or something else entirely.

While the conceptual steps above show you how to parse the data, you’ll put these into practice in your code soon. Often, you’ll see external libraries or frameworks handle this behind the scenes (e.g., Express.js has built-in body parsing). But understanding the manual approach gives you a deeper appreciation for what’s happening under the hood.

What You’ve Learned

In this article, you explored how to read and parse the request body with Node’s http package. You learned that requests arrive as streams of data chunks, and you must listen for data and end events to collect the full request body. Once you have that raw data, you can parse it according to the Content-Type (in this case, application/x-www-form-urlencoded)—splitting, decoding, and turning it into a convenient JavaScript object.

Armed with this knowledge, you can now handle form submissions and more complex requests with ease. The next time you see a long string with strange symbols and % codes, you’ll know exactly how to translate it into something meaningful for your application. You are, in essence, a message translator—bridging the gap between raw incoming data and the polished, user-friendly world of JavaScript objects.