-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathHttpInputBindingByIdInBody.cs
46 lines (44 loc) · 1.55 KB
/
HttpInputBindingByIdInBody.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
namespace cosmosdbinputbinding
{
using System.Net;
using System.Net.Http;
using System.Text;
using Microsoft.Azure.Documents;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
/// <summary>
/// This sample binds the HTTP Trigger body to a dynamic JSON object and uses one of its attributes to lookup by Id.
/// </summary>
/// <remarks>Sample payload is:
/// {
/// "Query": {
/// "Id":"SomeId"
/// }
/// }
/// </remarks>
public static class HttpInputBindingByIdInBody
{
[FunctionName("HttpInputBindingByIdInBody")]
public static HttpResponseMessage Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestMessage req,
[CosmosDB(databaseName: "%CosmosDBDatabase%",
collectionName: "%CosmosDBCollection%",
ConnectionStringSetting = "CosmosDBConnectionString",
Id = "{Query.Id}",
PartitionKey = "{Query.Id}")] Document document,
ILogger log)
{
if (document == null)
{
return new HttpResponseMessage(HttpStatusCode.NotFound);
}
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(JsonConvert.SerializeObject(document), Encoding.UTF8, "application/json")
};
}
}
}