0
0
Svelteframework~20 mins

Request parsing in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Request Parsing 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 Svelte component when submitting the form?

Consider this Svelte component that parses a form submission request. What will be logged to the console when the user submits the form with name="Alice" and age="30"?

Svelte
  <script>
    let name = '';
    let age = '';

    async function handleSubmit(event) {
      event.preventDefault();
      const formData = new FormData(event.target);
      const data = Object.fromEntries(formData.entries());
      console.log(data);
    }
  </script>

  <form on:submit={handleSubmit} aria-label="User info form">
    <label for="name">Name:</label>
    <input id="name" name="name" bind:value={name} required />

    <label for="age">Age:</label>
    <input id="age" name="age" type="number" bind:value={age} required />

    <button type="submit">Submit</button>
  </form>
ASyntaxError due to missing await
B{"name": "Alice", "age": "30"}
C{"name": "", "age": ""}
D{"name": "Alice", "age": 30}
Attempts:
2 left
💡 Hint

Remember that FormData entries are always strings.

📝 Syntax
intermediate
2:00remaining
Which option correctly parses JSON request body in a SvelteKit endpoint?

Given a SvelteKit endpoint, which code snippet correctly parses the JSON body from the request?

Svelte
export async function POST({ request }) {
  // parse JSON body here
}
Aconst data = await request.json(); return new Response(JSON.stringify(data));
Bconst data = JSON.parse(request.body); return new Response(JSON.stringify(data));
Cconst data = request.body.json(); return new Response(JSON.stringify(data));
Dconst data = await request.text(); return new Response(JSON.parse(data));
Attempts:
2 left
💡 Hint

Check the correct method to parse JSON from the request object.

🔧 Debug
advanced
2:00remaining
Why does this SvelteKit endpoint fail to parse form data correctly?

Examine this SvelteKit POST endpoint code. Why does it fail to parse the form data sent as application/x-www-form-urlencoded?

Svelte
export async function POST({ request }) {
  const data = await request.json();
  return new Response(JSON.stringify(data));
}
ABecause request.json() cannot parse urlencoded form data, it expects JSON content type.
BBecause request.body is empty and needs to be read as text first.
CBecause the endpoint must use request.formData() instead of request.json() for form data.
DBecause the response must be a redirect, not a JSON response.
Attempts:
2 left
💡 Hint

Consider the content type of the request and the method used to parse it.

state_output
advanced
2:00remaining
What is the value of the variable 'parsed' after this SvelteKit endpoint runs?

Given this SvelteKit POST endpoint, what is the value of parsed after execution if the request body is JSON {"color":"blue","count":5}?

Svelte
export async function POST({ request }) {
  const parsed = await request.json();
  return new Response(JSON.stringify(parsed));
}
Aundefined
B{"color":"blue","count":"5"}
C{"color":"blue","count":5}
DSyntaxError
Attempts:
2 left
💡 Hint

JSON parsing preserves data types for numbers and strings.

🧠 Conceptual
expert
3:00remaining
Which option correctly explains how SvelteKit handles request parsing for different content types?

In SvelteKit, how does the framework handle parsing of incoming request bodies for different content types like JSON and form data?

ASvelteKit automatically parses all request bodies and provides a unified object in the endpoint handler.
BSvelteKit only supports JSON request bodies and throws errors for other content types.
CSvelteKit requires manual parsing of the raw request body stream for all content types using <code>request.text()</code>.
DSvelteKit provides methods like <code>request.json()</code> for JSON and <code>request.formData()</code> for form data, which must be called explicitly to parse the body accordingly.
Attempts:
2 left
💡 Hint

Think about the methods available on the request object for parsing.