-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhttpPostBodyFunction.ts
43 lines (36 loc) · 1.24 KB
/
httpPostBodyFunction.ts
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
import { app, HttpRequest, HttpResponseInit, InvocationContext } from "@azure/functions";
interface Person {
name: string;
age: number;
}
function isPerson(obj: any): obj is Person {
return typeof obj === 'object' && obj !== null &&
typeof obj.name === 'string' &&
typeof obj.age === 'number';
}
export async function httpPostBodyFunction(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
context.log(`Http function processed request for url "${request.url}"`);
try {
const data: any = await request.json();
if (!isPerson(data)) {
return {
status: 400,
body: 'Please provide both name and age in the request body.'
};
}
return {
status: 200,
body: `Hello, ${data.name}! You are ${data.age} years old.`
};
} catch (error) {
return {
status: 400,
body: 'Invalid request body. Please provide a valid JSON object with name and age.'
};
}
};
app.http('httppost', {
methods: ['POST'],
authLevel: 'function',
handler: httpPostBodyFunction
});