-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathHelloWorldHttpTrigger4.cs
52 lines (46 loc) · 1.72 KB
/
HelloWorldHttpTrigger4.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Net;
using System.Threading.Tasks;
using System.Web;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
namespace AzFuncUni.Http
{
public class HelloWorldHttpTrigger4
{
private readonly ILogger _logger;
public HelloWorldHttpTrigger4(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<HelloWorldHttpTrigger4>();
}
[Function(nameof(HelloWorldHttpTrigger4))]
public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req)
{
_logger.LogInformation("C# HTTP trigger function processed a request.");
string name = default;
if (req.Method.Equals("get", StringComparison.OrdinalIgnoreCase))
{
var queryStringCollection = HttpUtility.ParseQueryString(req.Url.Query);
name = queryStringCollection["name"];
}
else
{
name = await req.ReadAsStringAsync();
}
var response = req.CreateResponse();
response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
if (string.IsNullOrEmpty(name))
{
response.StatusCode = HttpStatusCode.BadRequest;
await response.WriteStringAsync($"Please provide a value for the name query string parameter or in the body as plain text.");
}
else
{
response.StatusCode = HttpStatusCode.OK;
await response.WriteStringAsync($"Hello, {name}");
}
return response;
}
}
}