0
0
Svelteframework~20 mins

GET, POST, PUT, DELETE handlers in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SvelteKit HTTP Handlers Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What is the output of this Svelte GET handler?
Consider this SvelteKit GET handler code. What JSON response will the client receive?
Svelte
export async function GET() {
  return new Response(JSON.stringify({ message: 'Hello from GET' }), {
    headers: { 'Content-Type': 'application/json' }
  });
}
A{"message":"Hello from POST"}
B{"message":"Hello from GET"}
CSyntaxError: Unexpected token
D{"error":"Not found"}
Attempts:
2 left
💡 Hint
Look at the returned JSON string inside the Response.
state_output
intermediate
1:30remaining
What happens after this POST handler runs?
This SvelteKit POST handler receives JSON data and returns a modified response. What is the output JSON?
Svelte
export async function POST({ request }) {
  const data = await request.json();
  return new Response(JSON.stringify({ received: data.text.toUpperCase() }), {
    headers: { 'Content-Type': 'application/json' }
  });
}
A{"received":"HELLO"}
B{"received":"hello"}
CTypeError: Cannot read property 'toUpperCase' of undefined
D{"error":"Invalid data"}
Attempts:
2 left
💡 Hint
The handler converts the 'text' field to uppercase.
📝 Syntax
advanced
2:00remaining
Which PUT handler code is syntactically correct?
Choose the correct SvelteKit PUT handler syntax that updates a resource and returns status 204.
A
export async function PUT(request) {
  const data = await request.json();
  return new Response(null, { status: 204 });
}
B
export async function PUT({ request }) {
  const data = request.json();
  return new Response(null, { status: 204 });
}
C
export async function PUT({ request }) {
  const data = await request.json();
  // update logic here
  return new Response(null, { status: 204 });
}
D
export async function PUT({ request }) {
  const data = await request.json();
  return Response(null, { status: 204 });
}
Attempts:
2 left
💡 Hint
Check for correct parameter destructuring and await usage.
🔧 Debug
advanced
2:00remaining
Why does this DELETE handler cause a runtime error?
This SvelteKit DELETE handler throws an error. Identify the cause.
Svelte
export async function DELETE({ params }) {
  const id = params.id;
  if (!id) {
    return new Response('Missing ID', { status: 400 });
  }
  await database.delete(id);
  return new Response(null, { status: 204 });
}
AThe handler returns status 204 but should return 200
Bdatabase.delete is not awaited properly causing unhandled promise rejection
CResponse constructor is missing required body argument
Dparams is undefined because the handler lacks the correct route parameter setup
Attempts:
2 left
💡 Hint
Check if params is available in the handler context.
🧠 Conceptual
expert
1:30remaining
Which HTTP method handler should you use to partially update a resource in SvelteKit?
You want to update only some fields of a resource without replacing it entirely. Which handler method is best practice?
APATCH handler
BPUT handler
CPOST handler
DDELETE handler
Attempts:
2 left
💡 Hint
Consider HTTP methods semantics for partial updates.