Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 329
makerofthings7 Avatar asked Jun 20 '17 18:06

makerofthings7


People also ask

Can you trigger an Azure function using an HTTP request?

The HTTP trigger lets you invoke a function with an HTTP request. You can use an HTTP trigger to build serverless APIs and respond to webhooks. The default return value for an HTTP-triggered function is: HTTP 204 No Content with an empty body in Functions 2.

Where can you define the trigger bindings for an Azure function?

For triggers, the direction is always in. Input and output bindings use in and out. Some bindings support a special direction inout . If you use inout , only the Advanced editor is available via the Integrate tab in the portal.

Which option should be used to create HTTP triggered function in Azure?

Step-5: We need to choose the trigger for our Azure Function, so choose the Http trigger as the trigger option and For the Storage Account (AzureWebJobsStorage) option, select the Storage Emulator option and Choose Authorization level option as Anonymous On the Create a new Azure Functions Application window.


2 Answers

Based on the documentation from HTTP trigger from code, you can simply accept your own object:

For a custom type (such as a POCO), Functions will attempt to parse the request body as JSON to populate the object 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);
}
like image 104
Cloud SME Avatar answered Oct 06 '22 10:10

Cloud SME


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

like image 7
brettsam Avatar answered Oct 06 '22 11:10

brettsam