---
title: "How do I read the request body in a plain Node.js HTTP server?"
description: "The request body arrives as a stream of chunks, so you collect them and join them before parsing. Here is how to read JSON and form bodies safely with the node:http module."
url: https://www.danny.engineering/answer/nodejs/how-do-i-read-the-request-body-in-a-plain-nodejs-http-server
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T20:30:00.000Z
tags: ["Node.js"]
---

# How do I read the request body in a plain Node.js HTTP server?

## The short answer


The request object is a readable stream, so the body is not available straight away. Read all the chunks, join them, then parse. The shortest way is the `json` helper from `node:stream/consumers`.


```javascript
import { createServer } from 'node:http';
import { json } from 'node:stream/consumers';

createServer(async (req, res) => {
  const body = await json(req);
  res.end(`Hello ${body.name}`);
}).listen(3000);
```


## Why it happens


Frameworks like Express or Fastify give you `req.body` because they read and parse the body for you. The plain `node:http` module does not. It calls your handler as soon as the headers arrive, before the body has been received, so that large uploads can be processed piece by piece instead of being held in memory.


That is why `req.body` is `undefined` in a plain server. The data is there, but you have to read it from the stream.


## How to fix it


### Read the stream with for await


Requests are async iterable, so you can loop over the chunks. Each chunk is a `Buffer`.


```javascript
import { createServer } from 'node:http';

async function readBody(req, limit = 1_000_000) {
  const chunks = [];
  let size = 0;

  for await (const chunk of req) {
    size += chunk.length;
    if (size > limit) {
      const err = new Error('Body too large');
      err.status = 413;
      throw err;
    }
    chunks.push(chunk);
  }

  return Buffer.concat(chunks).toString('utf8');
}

createServer(async (req, res) => {
  try {
    const text = await readBody(req);
    res.end(`Received ${text.length} characters`);
  } catch (err) {
    res.statusCode = err.status ?? 500;
    res.end(err.message);
  }
}).listen(3000);
```


Joining buffers with `Buffer.concat` before turning them into a string matters. A multi-byte character such as an emoji can be split across two chunks, and converting each chunk separately would corrupt it.


The size limit matters too. Without it, anyone can send a huge body and fill your server's memory.


### Parse JSON safely


Check the content type and catch parse errors, so bad input gets a 400 response rather than crashing the handler.


```javascript
createServer(async (req, res) => {
  if (req.method !== 'POST') {
    res.writeHead(405).end();
    return;
  }

  if (!req.headers['content-type']?.startsWith('application/json')) {
    res.writeHead(415).end('Expected JSON');
    return;
  }

  let data;
  try {
    data = JSON.parse(await readBody(req));
  } catch {
    res.writeHead(400).end('Invalid JSON');
    return;
  }

  res.writeHead(201, { 'Content-Type': 'application/json' });
  res.end(JSON.stringify({ ok: true, received: data }));
}).listen(3000);
```


### Use node:stream/consumers for short code


The `node:stream/consumers` module has helpers named `text`, `json`, `buffer` and `arrayBuffer` that read a whole stream for you.


```javascript
import { text, json } from 'node:stream/consumers';

const raw = await text(req);
const data = await json(req); // use one or the other, a stream can only be read once
```


These are handy for scripts and internal tools. They do not apply a size limit, so for a public server, use your own reader with a limit as shown above.


### Parse HTML form posts


A normal HTML form sends `application/x-www-form-urlencoded`. Read the text, then parse it with `URLSearchParams`.


```javascript
const params = new URLSearchParams(await readBody(req));
const email = params.get('email');
```


File uploads use `multipart/form-data`, which is much harder to parse by hand. Use a library such as busboy for those.


## Common pitfalls

- **Reading the body twice.** A stream can only be consumed once. Read it into a variable and reuse that.
- **Responding before reading.** If you send a response without reading the body, the rest of the upload may be discarded or the connection closed. Read or drain it first.
- **Converting chunks to strings one at a time.** This breaks multi-byte characters. Collect buffers and join them.
- **No size limit.** Always cap the body size on a public server and reply with 413 when it is exceeded.
- **Forgetting GET requests.** GET and HEAD requests normally have no body. Check `req.method` before trying to parse one.

## Related reading

- [Node.js: Anatomy of an HTTP transaction](https://nodejs.org/en/learn/modules/anatomy-of-an-http-transaction)
- [Node.js: Stream consumers](https://nodejs.org/api/webstreams.html#utility-consumers)
