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"?
<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>Remember that FormData entries are always strings.
The FormData object returns all values as strings. So even though the age input is type number, its value is a string in the form data. The Object.fromEntries converts the entries into an object with string values.
Given a SvelteKit endpoint, which code snippet correctly parses the JSON body from the request?
export async function POST({ request }) { // parse JSON body here }
Check the correct method to parse JSON from the request object.
The request.json() method returns a promise resolving to the parsed JSON object. Option A uses it correctly with await. Option A is invalid because request.body is a ReadableStream, not an object with a json method. Option A tries to parse a stream directly, which is invalid. Option A parses text but returns a Response with a parsed object instead of string.
Examine this SvelteKit POST endpoint code. Why does it fail to parse the form data sent as application/x-www-form-urlencoded?
export async function POST({ request }) { const data = await request.json(); return new Response(JSON.stringify(data)); }
Consider the content type of the request and the method used to parse it.
The request.json() method only works if the request content type is JSON. For application/x-www-form-urlencoded data, you must use request.formData() to parse it correctly. Option A correctly identifies the cause.
Given this SvelteKit POST endpoint, what is the value of parsed after execution if the request body is JSON {"color":"blue","count":5}?
export async function POST({ request }) { const parsed = await request.json(); return new Response(JSON.stringify(parsed)); }
JSON parsing preserves data types for numbers and strings.
The request.json() method parses the JSON body into a JavaScript object preserving types. The number 5 remains a number, not a string.
In SvelteKit, how does the framework handle parsing of incoming request bodies for different content types like JSON and form data?
Think about the methods available on the request object for parsing.
SvelteKit provides convenient methods on the request object to parse different content types explicitly. request.json() parses JSON bodies, and request.formData() parses form data. It does not automatically parse all bodies or require manual stream reading for common types.