Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Azure Serverless Computing Cookbook

You're reading from   Azure Serverless Computing Cookbook Build applications hosted on serverless architecture using Azure Functions

Arrow left icon
Product type Paperback
Published in Aug 2017
Publisher Packt
ISBN-13 9781788390828
Length 332 pages
Edition 1st Edition
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Praveen Kumar Sreeram Praveen Kumar Sreeram
Author Profile Icon Praveen Kumar Sreeram
Praveen Kumar Sreeram
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Accelerate Your Cloud Application Development Using Azure Function Triggers and Bindings FREE CHAPTER 2. Working with Notifications Using SendGrid and Twilio Services 3. Seamless Integration of Azure Functions with Other Azure Services 4. Understanding the Integrated Developer Experience of Visual Studio Tools for Azure Functions 5. Exploring Testing Tools for the Validation of Azure Functions 6. Monitoring and Troubleshooting Azure Serverless Services 7. Code Reusability and Refactoring the Code in Azure Functions 8. Developing Reliable and Durable Serverless Applications Using Durable Functions 9. Implement Best Practices for Azure Functions 10. Implement Continuous Integration and Deployment of Azure Functions Using Visual Studio Team Services

Building a backend Web API using HTTP triggers

We will use Azure serverless architecture for building a Web API using HTTP triggers. These HTTP triggers could be consumed by any frontend application that is capable of making HTTP calls.

Getting ready

Let's start our journey of understanding Azure serverless computing using Azure Functions by creating a basic backend Web API that responds to HTTP requests:

We will be using C# as the programming language throughout the book.

How to do it…

  1. Navigate to the Function App listing page. Choose the function app in which you would like to add a new function.
  2. Create a new function by clicking on the + icon as shown in the following screenshot:
  1. If you have created a brand new function, then clicking on the + icon in the preceding step, you would see the Get started quickly with a premade function page. Please click on the create your own custom functions link to navigate to the page where you can see all the built-in templates for creating your Azure Functions.
  2. In the Choose a template below or go to the quickstart section, choose HTTPTrigger-CSharp as shown in the following screenshot to create a new HTTP trigger function:
  1. Provide a meaningful name. For this example, I have used RegisterUser as the name of the Azure Function.
  2. In the Authorization level drop-down, choose the Anonymous option as shown in the following screenshot. We will learn more about the all the authorization levels in Chapter 9, Implement Best Practices for Azure Functions:
  1. Once you provide the name and choose the Authorization level, click on Create button to create the HTTP trigger function.
  2. As soon as you create the function, all the required code and configuration files will be created automatically and the run.csx file will be opened for you to edit the code. Remove the default code and replace it with the following code:
        using System.Net;
public static async Task<HttpResponseMessage>
Run(HttpRequestMessage req, TraceWriter log)
{
string firstname=null,lastname = null;
dynamic data = await req.Content.ReadAsAsync<object>();
firstname = firstname ?? data?.firstname;
lastname = data?.lastname;
return (lastname + firstname) == null ?
req.CreateResponse(HttpStatusCode.BadRequest,
"Please pass a name on the query string or in the
request body")
:
req.CreateResponse(HttpStatusCode.OK, "Hello " +
firstname + " " + lastname);
}
  1. Save the changes by clicking on the Save button available just above the code editor.
  2. Let's try to test the RegisterUser function using the Test console. Click on the tab named Test as shown in the following screenshot to open the Test console:
  1. Enter the values for firstname and lastname, in the Request body section as shown in the following screenshot:

Please make sure you select POST in the HTTP method drop-down.

  1. Once you have reviewed the input parameters, click on the Run button available at the bottom of the Test console as shown in the following screenshot:
  1. If the input request workload is passed correctly with all the required parameters, you will see a Status 200 OK, and the output in the Output window will be as shown in the preceding screenshot.

How it works…

We have created the first basic Azure Function using HTTP triggers and made a few modifications to the default code. The code just accepts firstname and lastname parameters and prints the name of the end user with a Hello {firstname} {lastname} message as a response. We have also learnt how to test the HTTP trigger function right from the Azure Management portal.

For the sake of simplicity, I didn't perform validations of the input parameter. Please make sure that you validate all the input parameters in your applications running on your production environment.

See also

  • The Enabling authorization for function apps recipe in Chapter 9, Implement Best Practices for Azure Functions
You have been reading a chapter from
Azure Serverless Computing Cookbook
Published in: Aug 2017
Publisher: Packt
ISBN-13: 9781788390828
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image