Challenge - 5 Problems
Response Formatting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What will this Next.js API route return?
Consider this Next.js API route handler. What is the JSON response sent to the client?
NextJS
export async function GET() { return new Response(JSON.stringify({ message: 'Hello, world!' }), { status: 200, headers: { 'Content-Type': 'application/json' } }); }
Attempts:
2 left
💡 Hint
Look at the JSON.stringify argument and the key used.
✗ Incorrect
The handler returns a Response with JSON stringified object { message: 'Hello, world!' }. So the JSON response is {"message":"Hello, world!"}.
📝 Syntax
intermediate2:00remaining
Which option correctly formats a JSON response in Next.js API route?
You want to return a JSON response with status 201 and a body { success: true }. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Check how Response constructor expects arguments and headers.
✗ Incorrect
Option B correctly uses new Response with JSON stringified body, status, and headers. Option B is invalid because Response.json does not exist. Option B passes an object directly which causes error. Option B passes status as second argument without headers, invalid.
🔧 Debug
advanced2:00remaining
Why does this Next.js API route return a plain text response instead of JSON?
Look at this code snippet. Why does the client receive plain text instead of JSON?
NextJS
export async function GET() { return new Response({ message: 'Hi' }, { status: 200 }); }
Attempts:
2 left
💡 Hint
Check what happens when Response body is an object instead of a string.
✗ Incorrect
Response expects a string or a stream as body. Passing an object causes it to call toString(), resulting in '[object Object]' plain text. No JSON is sent.
❓ state_output
advanced2:00remaining
What is the output of this Next.js API route with headers?
Given this API route, what is the value of the 'X-Custom-Header' in the response?
NextJS
export async function GET() { return new Response(JSON.stringify({ data: 123 }), { status: 200, headers: { 'Content-Type': 'application/json', 'X-Custom-Header': 'MyValue' } }); }
Attempts:
2 left
💡 Hint
Look at the headers object keys and values.
✗ Incorrect
The header 'X-Custom-Header' is set to 'MyValue' exactly as given in the headers option.
🧠 Conceptual
expert2:00remaining
What error occurs if you forget to set Content-Type for JSON response in Next.js API route?
If you return JSON stringified data in a Response but omit the 'Content-Type' header, what will happen when the client tries to parse the response as JSON?
Attempts:
2 left
💡 Hint
Think about how browsers and clients use Content-Type to interpret response data.
✗ Incorrect
Without Content-Type: application/json, clients may not parse the response as JSON automatically, leading to errors or unexpected behavior.