tezvyn:

How do you parse JSON body in a POST Route Handler?

AI-drafted, machine-checkedSource: nextjs.orgbeginner

It tests whether you know Next.js App Router Route Handlers use the standard Web Request API. Call await request.json() inside the POST function; handle errors with try-catch and return a 400 for invalid JSON.

WHAT THIS TESTS: This question checks whether you understand that Next.js App Router Route Handlers are built on top of standard Web APIs rather than Node.js or Express conventions. The interviewer wants to see that you know the request object is a standard Request instance and that you are comfortable with modern async patterns for reading streams.

A GOOD ANSWER COVERS: A strong answer hits four things in order. First, export an async POST function that receives a request object. Second, call await request.json() to parse the JSON body because the Request prototype provides this method natively. Third, wrap the call in a try-catch since invalid JSON will throw a SyntaxError. Fourth, return a standard Response object, often with a 400 status code if parsing fails, rather than calling res.status or res.json.

COMMON WRONG ANSWERS: The biggest red flag is reaching for Express patterns such as req.body, body-parser, or res.json(). Another mistake is forgetting to await request.json() and treating the promise as the parsed data. Some candidates also suggest manually reading the stream with request.body.getReader() which is unnecessarily low level for JSON. Finally, importing NextApiRequest or NextApiResponse signals confusion with the older Pages Router API routes.

LIKELY FOLLOW-UPS: An interviewer might ask how you would handle a large JSON payload or streaming data, which could lead to discussing request.clone() or reading the stream directly. They might also ask about content-type validation, where you would check request.headers.get('content-type') before parsing. Another follow-up is error handling specifics, such as distinguishing between a 400 for bad JSON and a 415 for an unsupported media type.

ONE CONCRETE EXAMPLE: Imagine a Route Handler at app/api/user/route.js. The file exports async function POST(request). Inside, you write const body = await request.json(). If the client sends a name field, you destructure it and return Response.json({ created: true, name: body.name }). If request.json() throws, you catch it and return new Response('Invalid JSON', { status: 400 }). This shows you understand the standard Request and Response objects, async parsing, and proper HTTP status codes without any external middleware.

Read the original → nextjs.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.