Consider this Next.js route handler in the app/api/webhook/route.ts file. What will be the response when a POST request with JSON body {"event":"payment_succeeded"} is sent?
import { NextResponse } from 'next/server'; export async function POST(request: Request) { const data = await request.json(); if (data.event === 'payment_succeeded') { return NextResponse.json({ status: 'success', message: 'Payment processed' }); } return NextResponse.json({ status: 'ignored' }); }
Check how the JSON body is read and how the condition matches the event.
The handler reads the JSON body with await request.json(). If the event is payment_succeeded, it returns a JSON response with status success and a message. Otherwise, it returns ignored.
Identify which code snippet will cause a syntax error when used as a Next.js route handler for a webhook.
Look for missing semicolons or incorrect method calls.
Option D is incorrect because Response.json is not a valid method. The correct way is to use NextResponse.json or create a new Response with JSON string and appropriate headers. Option D is valid JavaScript as semicolons are optional if line breaks are present.
Given this route handler code, why does it fail to return 'pong' when a POST request with JSON body {"event":"ping"} is made?
export async function POST(request: Request) { const data = request.json(); if (data.event === 'ping') { return new Response('pong'); } return new Response('ignored'); }
Check how asynchronous functions and promises are handled.
The request.json() method returns a Promise. Without await, data is a Promise object, so data.event is undefined, causing the if condition to fail and return 'ignored'.
Analyze this route handler code. What will be the response body if a GET request is sent to this webhook route?
import { NextResponse } from 'next/server'; export async function POST(request: Request) { return NextResponse.json({ message: 'POST received' }); } export async function GET(request: Request) { return NextResponse.json({ message: 'GET not supported' }); }
Check which function handles GET requests.
The GET function returns a JSON response with message 'GET not supported'. So a GET request will receive that JSON body.
You want to create a Next.js route handler for a webhook that first checks the X-Signature header matches a secret before reading the JSON body. Which code snippet correctly implements this?
Remember how to get headers in Next.js route handlers and the correct status code for unauthorized access.
Option C correctly uses request.headers.get() with the exact header name, compares it to the secret, returns 401 Unauthorized if it doesn't match, then processes the JSON body.
Option C uses incorrect header access syntax. Option C uses lowercase header name and 403 status code which is less appropriate here. Option C reverses the condition causing wrong authorization logic.