0
0
NextJSframework~20 mins

HTTP method handlers (GET, POST) in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next.js HTTP Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Next.js API route GET handler?
Consider this Next.js API route handler. What will the client receive when making a GET request?
NextJS
export async function GET(request) {
  return new Response(JSON.stringify({ message: 'Hello GET' }), {
    status: 200,
    headers: { 'Content-Type': 'application/json' }
  });
}
A{"message":"Hello GET"}
BSyntaxError
C{"error":"Method not allowed"}
DHello GET
Attempts:
2 left
💡 Hint
Look at how the response is constructed with JSON.stringify and content type.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a POST handler in Next.js API route?
Select the option that correctly defines a POST handler function in Next.js API route file.
A
export async function POST() {
  return new Response('No request param');
}
B
export async function POST(request) {
  const data = await request.json();
  return new Response(JSON.stringify({ received: data }), { status: 201 });
}
C
export function post(request) {
  return new Response('OK');
}
D
export async function POST(request) {
  return 'OK';
}
Attempts:
2 left
💡 Hint
Remember the handler must be named exactly POST and accept the request parameter.
state_output
advanced
2:00remaining
What is the response status code after this POST handler runs?
Given this POST handler, what status code will the client receive?
NextJS
export async function POST(request) {
  const body = await request.json();
  if (!body.name) {
    return new Response('Missing name', { status: 400 });
  }
  return new Response('Created', { status: 201 });
}
A201
B200
C400
D500
Attempts:
2 left
💡 Hint
Check the condition for missing name and what happens if name exists.
🔧 Debug
advanced
2:00remaining
Which option causes a runtime error in this Next.js API route?
Identify which POST handler option will cause a runtime error when called.
A
export async function POST(request) {
  const data = await request.json();
  return new Response(JSON.stringify(data));
}
B
export async function POST(request) {
  const data = await request.text();
  return new Response(data);
}
C
export async function POST() {
  const data = await request.json();
  return new Response(JSON.stringify(data));
}
D
export async function POST(request) {
  return new Response('OK');
}
Attempts:
2 left
💡 Hint
Check if the function parameters match usage inside the function.
🧠 Conceptual
expert
2:00remaining
Why should Next.js API route handlers return a Response object instead of a plain string?
Choose the best explanation for why Next.js API route handlers like GET or POST should return a Response object rather than just a string.
AResponse objects are required only for POST, not GET handlers.
BReturning strings causes Next.js to crash immediately.
CStrings are automatically converted to JSON, which is not always desired.
DResponse objects allow setting status codes and headers, which strings alone cannot provide.
Attempts:
2 left
💡 Hint
Think about what extra information a Response object can carry.