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?
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.
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.
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.
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);
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With