How can I do ModelBinding with HttpTrigger in Azure Functions?

I need to create an Azure Function that responds to a HTTP POST, and leverages the integrated model binding.

How can I modify 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 POST’s the object, and I have normal ASP.NET style model binding?

Answer

Instead of using an HttpRequestMessage parameter, you can use a custom type. The binding will attempt to parse the request body as JSON and populate that object before calling the function. Some details here: https://docs.microsoft.com/azure/azure-functions/functions-bindings-http-webhook-trigger?tabs=csharp#payload

Leave a Reply

Your email address will not be published. Required fields are marked *