How to pass parameters by POST to an Azure function?

前端 未结 9 2003
暗喜
暗喜 2020-12-24 05:27

I\'m trying to do a simple Azure Function to learn about it. There will be 3 functions:

  • 1 function to insert a row into a table of a database. This table will
9条回答
  •  星月不相逢
    2020-12-24 06:07

    I have done a very simple example to get data using POST request in Azure Function App. Please find the following example.

    using System;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Threading.Tasks;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Extensions.Http;
    using Microsoft.Azure.WebJobs.Host;
    
    namespace MyFunctions
    {
        public static class MyFunctionsOperations
        {
            [FunctionName("MyFunctionsOperations")]
            public static async Task Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]HttpRequestMessage req, TraceWriter log)
            {
                log.Info("C# HTTP trigger function processed a request.");
                var headers = req.Headers;
                string collection = headers.GetValues("collection").First();   //getting parameter from header
    
                CosmosdbOperation obj = new CosmosdbOperation();
                dynamic data = await req.Content.ReadAsAsync();  //getting body content
                Boolean response = await obj.MyFunctionExecution(data.ToString(), collection);
    
                return (response)
                    ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a proper argument in the request body")
                    : req.CreateResponse(HttpStatusCode.OK, "Operation successfully executed..");
            }
        }
    }
    
        

    提交回复
    热议问题