Complete the code to modify the request method to POST in a Next.js API route.
export async function handler(req, res) {
if (req.method === '[1]') {
res.status(200).json({ message: 'Success' });
} else {
res.status(405).end();
}
}The request method is checked against 'POST' to handle POST requests correctly.
Complete the code to read JSON data from the request body in a Next.js API route.
export async function handler(req, res) {
if (req.method === 'POST') {
const data = await req.[1]();
res.status(200).json({ received: data });
} else {
res.status(405).end();
}
}Use req.json() to parse JSON data from the request body.
Fix the error in the code to correctly modify the request headers in a Next.js middleware.
import { NextResponse } from 'next/server'; export function middleware(req) { const res = NextResponse.next(); res.headers.set('[1]', 'application/json'); return res; }
The header name must be 'Content-Type' with correct casing to set the response content type.
Fill both blanks to correctly clone and modify the request URL in Next.js middleware.
import { NextResponse } from 'next/server'; export function middleware(req) { const url = req.nextUrl.clone(); url.[1] = '/new-path'; return NextResponse.rewrite(url.[2]); }
Modify the pathname to change the path, and use href to get the full URL string for rewriting.
Fill all three blanks to correctly modify request headers and method in Next.js middleware.
import { NextResponse } from 'next/server'; export function middleware(req) { const newHeaders = new Headers(req.headers); newHeaders.set('[1]', 'application/json'); const newRequest = req.clone({ method: '[2]', headers: newHeaders }); return NextResponse.next({ request: newRequest, [3]: true }); }
Set the 'Content-Type' header, change the method to 'POST', and use 'waitUntil' to handle async tasks in middleware.