Complete the code to set a custom header in a Next.js API route response.
export async function GET() {
const response = new Response('Hello World');
response.headers.set('[1]', 'application/json');
return response;
}The Content-Type header tells the client what type of data is being sent. Here, setting it to application/json indicates JSON data.
Complete the code to modify the response status code in a Next.js API route.
export async function GET() {
return new Response('Not Found', { status: [1] });
}The status code 404 means 'Not Found', which matches the response message.
Fix the error in setting multiple headers in the Next.js response object.
export async function GET() {
const headers = new Headers();
headers.append('X-Custom-Header', 'value1');
headers.[1]('X-Another-Header', 'value2');
return new Response('OK', { headers });
}The Headers object uses set to add or overwrite a header. append adds a new value, but to set a new header, set is correct.
Fill both blanks to create a JSON response with a custom status code and header.
export async function GET() {
const data = { message: 'Success' };
return new Response(JSON.stringify(data), { status: [1], headers: { '[2]': 'application/json' } });
}Status 200 means success. The header Content-Type tells the client the response is JSON.
Fill all three blanks to create a Next.js API response with a JSON body, custom header, and status code.
export async function GET() {
const result = { success: true };
return new Response(JSON.stringify([1]), {
status: [2],
headers: { '[3]': 'application/json' }
});
}The response body is the result object stringified. Status 200 means success. Header Content-Type indicates JSON data.