Challenge - 5 Problems
Astro API Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediateWhat does this Astro server endpoint return?
Consider this Astro server endpoint code. What is the JSON response when a GET request is made?
Astro
export async function GET() { return new Response(JSON.stringify({ message: 'Hello from Astro!' }), { status: 200, headers: { 'Content-Type': 'application/json' } }); }
Attempts:
2 left
💡 Hint
Look carefully at the JSON key and exact string value returned.
✗ Incorrect
The endpoint returns a JSON object with key 'message' and value 'Hello from Astro!'. The response is stringified JSON with correct key and punctuation.
📝 Syntax
intermediateWhich option causes a syntax error in Astro API route?
Identify which server endpoint code snippet will cause a syntax error in Astro.
Attempts:
2 left
💡 Hint
Check function declaration syntax carefully.
✗ Incorrect
Option A is missing parentheses after DELETE in function declaration, causing a syntax error.
❓ state_output
advancedWhat is the response status code of this Astro API route?
Given this Astro server endpoint, what status code will the client receive?
Astro
export async function GET() { return new Response('Not Found', { status: 404 }); }
Attempts:
2 left
💡 Hint
Look at the status property in the Response options.
✗ Incorrect
The Response is created with status 404, so the client receives HTTP 404 Not Found.
🔧 Debug
advancedWhy does this Astro API route fail to parse JSON?
This Astro POST endpoint tries to parse JSON but throws an error. Why?
export async function POST({ request }) {
const data = await request.text();
const json = JSON.parse(data);
return new Response(JSON.stringify(json));
}
Attempts:
2 left
💡 Hint
Consider what happens if the request body is empty or invalid JSON.
✗ Incorrect
If the request body is empty, request.text() returns an empty string, and JSON.parse('') throws a SyntaxError.
🧠 Conceptual
expertHow does Astro handle multiple HTTP methods in one API route file?
In Astro, if you export multiple functions like GET, POST, and PUT in one API route file, how does Astro handle incoming requests?
Attempts:
2 left
💡 Hint
Think about how REST APIs usually handle different HTTP methods.
✗ Incorrect
Astro matches the HTTP method of the incoming request to the exported function with the same name. If no matching function exists, it returns 405.
