How to pass parameters by POST to an Azure function?

前端 未结 9 1981
暗喜
暗喜 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:09

    It can be done in following way with custom class

    Azure Function

    [FunctionName("PostParameterFunction")]
    public static async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, ILogger log)
       {
          log.LogInformation("C# HTTP trigger function processed a request.");
    
           try
            {
                 // Convert all request perameter into Json object
    
                    var content = req.Content;
                    string jsonContent = content.ReadAsStringAsync().Result;
                    dynamic requestPram = JsonConvert.DeserializeObject(jsonContent);
    
                    // Validate the required param
    
                    if (string.IsNullOrEmpty(requestPram.FirstName))
                    {
                        return req.CreateResponse(HttpStatusCode.OK, "Please enter First Name!");
                    }
                    if (string.IsNullOrEmpty(requestPram.LastName))
                    {
                        return req.CreateResponse(HttpStatusCode.OK, "Please enter Last Name!");
                    }
    
    
                    //Create object for partner Model to bind the response on it
    
                    RequestModel objRequestModel = new RequestModel();
    
                    objRequestModel.FirstName = requestPram.FirstName;
                    objRequestModel.LastName = requestPram.LastName;
    
                    //Return Request Model
    
                    return req.CreateResponse(HttpStatusCode.OK, objRequestModel);
             }
            catch (Exception ex)
             {
    
                    return req.CreateResponse(HttpStatusCode.OK, "Cannot Create Request! Reason: {0}", string.Format(ex.Message));
             }
    
            }
    

    Request Class:

     public class RequestModel
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
    
        }
    

    Request Input:

    {
        "FirstName": "Kiron",
        "LastName":"Test"
    }
    

    PostMan Output Example:

提交回复
热议问题