How do I get the raw body of a Serverless Function?

Learn how to disable body parsing for Vercel Serverless Functions to enable reading the raw data from a POST request.
Last updated on April 9, 2024
Functions

You can use Vercel Serverless Functions to recieve webhooks from third-party services like Stripe. Certain providers require access to the raw body inside the function to validate the signature from the request.

export async function POST(request: Request) {
const rawBody = await request.text()
return Response.json({ rawBody })
}
A Node.js Vercel Function that returns the raw body sent.

You can verify your function can accept and parse raw bodies locally:

  1. Start your local development server with vercel dev (or next dev).
  2. In another terminal window, run the cURL command below.
curl -X POST -H "Content-Type: text/plain" --data "This is raw data" http://localhost:3000/api/test
A sample cURL command to make a POST request to your function.

You should see a response like {"rawBody": "This is raw data"}.

Couldn't find the guide you need?