Complete the code to access the request method in an Astro endpoint.
export async function GET({ request }) {
const method = request.[1];
return new Response(`Method is ${method}`);
}The request.method property gives the HTTP method (GET, POST, etc.) of the request.
Complete the code to read JSON data from a POST request in an Astro endpoint.
export async function POST({ request }) {
const data = await request.[1]();
return new Response(JSON.stringify(data));
}Use request.json() to parse the JSON body of a POST request.
Fix the error in the code to set a custom header in the response.
export async function GET() {
return new Response('Hello', { headers: { [1]: 'application/json' } });
}HTTP headers are case-insensitive but usually written in standard format like Content-Type.
Fill both blanks to create a JSON response with status 201 and a custom header.
export async function POST() {
return new Response(JSON.stringify({ success: true }), { status: [1], headers: { [2]: 'application/json' } });
}Status 201 means resource created. Content-Type header tells the client the response is JSON.
Fill all three blanks to extract a query parameter and return it in the response.
export async function GET({ request }) {
const url = new URL(request.[1]);
const name = url.searchParams.get('[2]');
return new Response(`Hello, $[3]!`);
}Use request.url to get the full URL, then get the query parameter name, and use it in the response.
