-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Azure Serverless Computing Cookbook
By :

In this recipe, we'll use Azure's serverless architecture to build a web API using HTTP triggers. These HTTP triggers could be consumed by any front-end application that is capable of making HTTP calls.
Let's start our journey of understanding Azure serverless computing using Azure Functions by creating a basic back-end web API that responds to HTTP requests:
Remember the name of the storage account, as it will be used later in other chapters.
We'll be using C# as the programming language throughout the book. Most of the functions are developed using the Azure Functions V3 runtime. However, as of the time of writing, a few recipes were not supported in the V3 runtime. Hopefully, soon after the publication of this book, Microsoft will have made those features available for the V3 runtime as well.
Perform the following steps to build a web API using HTTP triggers:
RegisterUser
as the name of the Azure function.run.csx
file with editable code will get opened. Remove the default code and replace it with the following code. In the following example, we'll add two parameters (firstname
and lastname
), which will be displayed in the output as a result of triggering the HTTP trigger:#r "Newtonsoft.Json" using System.Net; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Primitives; using Newtonsoft.Json; public static async Task<IActionResult> Run( HttpRequest req, ILogger log) #r "Newtonsoft.Json" using System.Net; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Primitives; using Newtonsoft.Json; public static async Task<IActionResult> Run(HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string firstname=null,lastname = null; string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic inputJson = JsonConvert.DeserializeObject(requestBody); firstname = firstname ?? inputJson?.firstname; lastname = inputJson?.lastname; return (lastname + firstname) != null ? (ActionResult)new OkObjectResult($"Hello, {firstname + " " + lastname}") : new BadRequestObjectResult("Please pass a name on the query" + "string or in the request body"); }
RegisterUser
function using the test console. Click on the Test tab to open the test console:firstname
and lastname
in the Request body section:You have created your first Azure function using HTTP triggers and have made a few modifications to the default code. The code accepts the firstname
and lastname
parameters and prints the name of the end user with a Hello
{firstname
} {lastname
} message as a response. You also learned how to test the HTTP trigger function right from the Azure Management portal.
For the sake of simplicity, validation of the input parameters is not executed in this exercise. Be sure to validate all input parameters in applications running in a production environment.
In the next recipe, you'll learn about persisting employee details.