How can I make ModelBinding with HttpTrigger in Azure Functions?
I need to create an Azure Function that responds to HTTP POST and uses an integrated model binding.
How can I change this
[FunctionName("TokenPolicy")]
public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "TokenPolicy/{IssuerID}/{SpecificationID}")]HttpRequestMessage req, string IssuerID, string specificationID, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request. TokenPolicy");
// Fetching the name from the path parameter in the request URL
return req.CreateResponse(HttpStatusCode.OK, "data " + specificationID);
}
in such a way that my client is a POST object and I have a normal ASP.NET style model binding?
source to share
Based on the documentation for the HTTP trigger from code, you can simply accept your own object:
For a custom type (like POCO), the functions will try to parse the request body as JSON to populate the object's properties.
public class MyModel
{
public int Id { get; set; }
public string Name { get; set; }
}
[FunctionName("TokenPolicy")]
public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "TokenPolicy/{IssuerID}/{SpecificationID}")]MyModel myObj, string IssuerID, string specificationID, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request. TokenPolicy");
// Do something your your object
return new HttpResponseMessage(HttpStatusCode.OK);
}
source to share
Instead of using a parameter, HttpRequestMessage
you can use a non-standard type. The binding will try to parse the request body as JSON and populate this object before calling the function. Some details here: https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook#http-trigger
source to share