How do you read request body in Route Handlers?

Call the Web Request body methods: await request.json() for JSON, await request.formData() for forms and file uploads, await request.text() for raw text (webhook signatures), await request.arrayBuffer() or request.blob() for binary, or read request.body as a stream. A body can be read only once.

Choosing the method

Content-TypeMethod
application/jsonrequest.json()
multipart/form-data, application/x-www-form-urlencodedrequest.formData()
Webhooks that verify a signaturerequest.text() (raw string)
Binary uploadsrequest.arrayBuffer() / blob()
Large or streaming uploadsrequest.body (ReadableStream)

Rules and pitfalls

  • The body is a stream: calling json() after text() throws "body used already". Use request.clone() if you must read it twice.
  • request.json() throws on invalid JSON; wrap it in try/catch and return 400.
  • Always validate the parsed data (Zod); never trust its shape.
  • There is no automatic body size limit in Route Handlers like Pages Router's 1MB bodyParser; enforce limits yourself (check Content-Length or count bytes while streaming), and remember your host may cap request size.
  • GET and HEAD requests have no body; use search params instead.

Pages Router comparison

req.body is pre-parsed by bodyParser. Disable it (config.api.bodyParser = false) to get the raw stream for signatures.

Code example

tsx
1// app/api/contact/route.ts
2import { z } from "zod";
3
4const Contact = z.object({
5 email: z.string().email(),
6 message: z.string().min(10).max(5000),
7});
8
9export async function POST(request: Request) {
10 const type = request.headers.get("content-type") ?? "";
11
12 let input: unknown;
13 try {
14 input = type.includes("application/json")
15 ? await request.json()
16 : Object.fromEntries(await request.formData()); // works for HTML forms too
17 } catch {
18 return Response.json({ error: "Invalid body" }, { status: 400 });
19 }
20
21 const parsed = Contact.safeParse(input);
22 if (!parsed.success) {
23 return Response.json({ error: parsed.error.flatten().fieldErrors }, { status: 422 });
24 }
25
26 // await sendEmail(parsed.data);
27 return Response.json({ ok: true }, { status: 201 });
28}

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app