Web Request parsing and Response/NextResponse.
Skip to solutionKEEP THE
mediumFrontend
How do you read request data and return responses in a Next.js Route Handler?
570 views
01
Understand the problem
nextjsroute-handlersrequestresponse
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Nudge consolestandby
Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.
03
Study the solution
A handler receives a standard Request: use await request.json()/formData(), request.nextUrl.searchParams, cookies(), and headers(). Return a Response or NextResponse (e.g. NextResponse.json(data, { status })). Dynamic params come from the second argument's params.
Solution ready — 2 min read
Classified // press E to declassify
04
Read the code
Dynamic param + query + JSON response
// app/api/users/[id]/route.ts
import { NextResponse } from 'next/server';
export async function GET(request: Request,
{ params }: { params: Promise<{ id: string }> }) {
const { id } = await params; // dynamic segment
const q = request.nextUrl.searchParams.get('fields'); // query string
const user = await db.user.find(id);
if (!user) return NextResponse.json({ error: 'not found' }, { status: 404 });
return NextResponse.json(user);
}05
Join the discussion
Discussion (0)
Sign in to join the discussion.
No responses yet. Be the first to share what you think.
Transmission complete // awaiting log
KEEP THE
STREAK ALIVE.
Dossier 50 of 95 decoded in the Next.js track. One more won't hurt.