1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
import type { APIRoute } from 'astro';
export const GET: APIRoute = async ({ request }) => {
const url = new URL(request.url);
const name = url.searchParams.get('name') || 'World';
return new Response(
JSON.stringify({
message: `Hello, ${name}!`,
timestamp: new Date().toISOString(),
method: 'GET'
}),
{
status: 200,
headers: {
'Content-Type': 'application/json',
},
}
);
};
export const POST: APIRoute = async ({ request }) => {
try {
const body = await request.json();
return new Response(
JSON.stringify({
message: 'Data received successfully',
received: body,
timestamp: new Date().toISOString(),
method: 'POST'
}),
{
status: 200,
headers: {
'Content-Type': 'application/json',
},
}
);
} catch (error) {
return new Response(
JSON.stringify({
error: 'Invalid JSON',
timestamp: new Date().toISOString()
}),
{
status: 400,
headers: {
'Content-Type': 'application/json',
},
}
);
}
};
|