forked from Azure-Samples/functions-quickstart-dotnet-azd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpPostBodyFunction.cs
38 lines (32 loc) · 1.58 KB
/
httpPostBodyFunction.cs
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
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using FromBodyAttribute = Microsoft.Azure.Functions.Worker.Http.FromBodyAttribute;
namespace Company.Function
{
public class HttpPostBody
{
private readonly ILogger _logger;
public HttpPostBody(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<HttpPostBody>();
}
[Function("httppost")]
public IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req,
[FromBody] Person person)
{
_logger.LogInformation("C# HTTP POST trigger function processed a request for url {body}", req.Body);
if (string.IsNullOrEmpty(person.Name) | string.IsNullOrEmpty(person.Age.ToString()) | person.Age == 0)
{
_logger.LogInformation("C# HTTP POST trigger function processed a request with no name/age provided.");
return new BadRequestObjectResult("Please provide both name and age in the request body.");
}
var returnValue = $"Hello, {person.Name}! You are {person.Age} years old.";
_logger.LogInformation("C# HTTP POST trigger function processed a request for {Name} who is {Age} years old.", person.Name, person.Age);
return new OkObjectResult(returnValue);
}
}
public record Person([property: JsonPropertyName("name")] string Name, [property: JsonPropertyName("age")] int Age);
}