Complete the code to export a POST route handler for a webhook in Next.js.
export async function [1](request) { const data = await request.json(); return new Response('Webhook received', { status: 200 }); }
The POST method is used to handle webhook requests because webhooks usually send data to your server.
Complete the code to parse JSON body from the webhook request.
export async function POST(request) {
const [1] = await request.json();
return new Response('Data processed', { status: 200 });
}We assign the parsed JSON to a variable named data to represent the webhook payload.
Fix the error in the route handler to correctly respond with JSON content type.
export async function POST(request) {
const data = await request.json();
return new Response(JSON.stringify({ message: 'Success' }), { headers: { '[1]': 'application/json' }, status: 200 });
}The header Content-Type must be capitalized exactly to tell the client the response is JSON.
Fill both blanks to create a route handler that responds with status 400 if no JSON body is sent.
export async function POST(request) {
try {
const data = await request.[1]();
if (!data) {
return new Response('Bad Request', { status: [2] });
}
return new Response('OK', { status: 200 });
} catch {
return new Response('Bad Request', { status: 400 });
}
}We use request.json() to parse JSON body and respond with status 400 for bad requests.
Fill all three blanks to create a route handler that logs the webhook event type and returns a JSON response.
export async function POST(request) {
const [1] = await request.json();
console.log('Received event:', [2]);
return new Response(JSON.stringify({ success: true, event: [3] }), {
headers: { 'Content-Type': 'application/json' },
status: 200
});
}The JSON body is stored in payload. We log and return the type property from it.