Next.js · Junior · Question 63 of 75

How do you handle POST /api/posts in a Route Handler?

// app/api/posts/route.ts
export async function POST(request: Request) {
  const body = await request.json()
  return Response.json(body, { status: 201 })
}

Explanation

Each HTTP method is a named export: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS. Unsupported methods get a 405 response.

Further reading

Problem with a question

Next.js · Junior · question 63

Test yourself

Take the Next.js · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz