# Handling Node.js Request Bodies with Vercel

**Author:** Matthew Sweeney

---

In this guide, we will show you how to parse a [Node.js request body](https://nodejs.org/learn/http/anatomy-of-an-http-transaction#anatomy-of-an-http-transaction), for use inside a [Serverless Function](https://vercel.com/docs/concepts/functions#serverless-functions) deployed to [Vercel](https://vercel.com/), without requiring a framework such as Express.

This guide assumes the request is sent with a [Content-Type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type) of [application/json](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON). However, many more content types can be parsed if required.

## Step 1: Creating the Function

To get started, create a project directory with an `/api` directory inside and `cd` into it:

`mkdir req-body && mkdir req-body/api cd req-body`

To illustrate the parsing of a request body, create an `index.js` file inside your `/api` directory with the following code:

``export default async function handler(req, res) { const { body } = req; return res.send(`Hello ${body.name}, you just parsed the request body!`); }``

This function takes a POST request, parses the body, and uses data from the body in the response.

The key part here is line 2. [Vercel](https://vercel.com/) adds a number of Express like [helper properties](https://vercel.com/docs/concepts/functions/serverless-functions/runtimes/node-js#node.js-helpers) to make wording with Node.js even easier.

> \*\*Note: \*\*You can read more about [helper properties](https://vercel.com/docs/concepts/functions/serverless-functions/runtimes/node-js#node.js-helpers) either in the [blog post](https://vercel.com/blog/vercel-node-helpers) or in the [documentation](https://vercel.com/docs/concepts/functions/serverless-functions/runtimes/node-js#node.js-helpers).

With the `req.body` helper, the incoming `request` is parsed automatically, removing the need for third-party libraries or further code to handle the [ReadableStream](https://nodejs.org/api/stream.html#stream_class_stream_readable) format the `request` normally arrives in.

## Step 2: Deploying the Function

Deploying your function to [Vercel](https://vercel.com/) can be done with **a single command**:

`vercel`

You have now created and deployed your project, all that's left to do is test that it works.

## Step 3: Sending the Request

To verify that the JSON is being parsed correctly, make a POST request to your new deployment using [curl](https://curl.haxx.se/) by executing the below code inside your terminal:

`curl -X POST "https://your-deployments-url.vercel.app/api" \ -H "Content-Type: application/json" \ -d '{ "name": "Reader" }'`

> \*\*Note: \*\*You should change the URL to match the one for your deployment given to you in the Vercel CLI.

You will receive a response similar to the following:

`Hello Reader, you just parsed the request body!`

Congratulations, now you know how to parse request bodies with [Vercel](https://vercel.com/) in Node.js!

## Bonus: Understanding Why this Works

When [Node.js](https://nodejs.org/docs/latest-v8.x/api/) receives a request, the [body](https://nodejs.org/learn/http/anatomy-of-an-http-transaction) is in the format of a [ReadableStream](https://nodejs.org/api/stream.html#stream_class_stream_readable).

To get the data from the stream, you need to listen to its `data` and `end` events. To do this requires a few lines of code which you would much rather not have to repeat.

By using [helper properties](https://vercel.com/docs/concepts/functions/serverless-functions/runtimes/node-js#node.js-helpers), the `request` is already parsed for you, allowing you to access the data immediately.

---

[View full KB sitemap](/kb/sitemap.md)
