Complete the code to send a JSON response with Next.js.
export async function GET() {
return new Response(JSON.stringify({ message: 'Hello' }), {
headers: { 'Content-Type': [1] }
});
}The header 'Content-Type': 'application/json' tells the browser the response is JSON.
Complete the code to return a 404 status with a JSON error message.
export async function GET() {
return new Response(JSON.stringify({ error: 'Not found' }), {
status: [1],
headers: { 'Content-Type': 'application/json' }
});
}Status code 404 means the requested resource was not found.
Fix the error in setting a custom header in the response.
export async function GET() {
return new Response('OK', {
headers: { [1]: 'no-cache' }
});
}Headers keys must be strings with correct casing, so use 'Cache-Control'.
Fill both blanks to create a JSON response with a 201 status and a custom header.
export async function POST() {
return new Response(JSON.stringify({ success: true }), {
status: [1],
headers: { [2]: 'application/json' }
});
}Status 201 means resource created. The header 'Content-Type' must be set to 'application/json' for JSON responses.
Fill all three blanks to return a JSON response with a 400 status, a custom error message, and the correct header.
export async function GET() {
const error = { message: [1] };
return new Response(JSON.stringify(error), {
status: [2],
headers: { [3]: 'application/json' }
});
}The error message is a string, status 400 means bad request, and 'Content-Type' header must be set to 'application/json'.