0
0
NextJSframework~20 mins

Response formatting in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Response Formatting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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' }
  });
}
A{"message":"Hello, world!"}
B"Hello, world!"
C{"msg":"Hello, world!"}
D{"message":"hello world"}
Attempts:
2 left
💡 Hint
Look at the JSON.stringify argument and the key used.
📝 Syntax
intermediate
2: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?
Areturn Response.json({ success: true }, { status: 201 });
Breturn new Response(JSON.stringify({ success: true }), { status: 201, headers: { 'Content-Type': 'application/json' } });
Creturn new Response({ success: true }, { status: 201 });
Dreturn new Response(JSON.stringify({ success: true }), 201);
Attempts:
2 left
💡 Hint
Check how Response constructor expects arguments and headers.
🔧 Debug
advanced
2: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 });
}
ABecause the body is an object, not a string, so Response converts it to plain text '[object Object]'.
BBecause the status code 200 is missing the JSON flag.
CBecause the Content-Type header is set to 'text/plain' by default.
DBecause the function is async but does not await anything.
Attempts:
2 left
💡 Hint
Check what happens when Response body is an object instead of a string.
state_output
advanced
2: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'
    }
  });
}
Aundefined
Bmyvalue
CX-Custom-Header
DMyValue
Attempts:
2 left
💡 Hint
Look at the headers object keys and values.
🧠 Conceptual
expert
2: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?
AThe server will throw a SyntaxError before sending the response.
BThe response will automatically have Content-Type set to 'application/json' by Next.js.
CThe client may fail to parse the response as JSON or treat it as plain text, causing runtime errors.
DThe client will receive a 500 Internal Server Error.
Attempts:
2 left
💡 Hint
Think about how browsers and clients use Content-Type to interpret response data.