How To Create An HTTP Trigger Azure Function App Using Visual Studio 2017

In this article, you will learn how to create an HTTP Trigger Azure Function App and read the JSON data.

Introduction

 
Azure Functions is a solution for easily running small pieces of code, or "functions", in the cloud. You can write just the code you need for the problem at hand, without worrying about a whole application or the infrastructure to run it. Details of the features of the Functions app are available at this link.
 

Features

Choice of language - You can choose your preferred programming language like C#, F# or JavaScript and see a link for more information for supported languages (With batch files, it can run anything).

Pay-per-use pricing model - You only pay for the time your code runs.

Bring your own dependencies - It supports NuGet and NPM, so you can use your favorite libraries.

Integrated security - Protect HTTP-triggered functions with OAuth providers such as Azure Active Directory, Facebook, Google, Twitter, and Microsoft Account.

Simplified integration - Azure Functions integrates with various Azure and third-party services. These services can trigger your function and start execution, or they can serve as input and output for your code. The following service integrations are supported by Azure Functions.

Flexible development - Code your functions right in the portal or set up continuous integration and deploy your code through GitHub, Azure DevOps Services, and other supported development tools.

Open-source - The function runtime is open-source

Monitoring - Through application insights, we can monitor the function apps.

Server less Architecture - Microsoft Azure Functions acts as a modern serverless architecture delivering event-driven cloud computing configured to comply with application development.

Now let’s create an Http-triggered Azure function App in Visual Studio.

About Http Triggered Function Apps

For HTTP-triggered functions, you can specify the authorization types needed to have in order to execute it. There are five types you can choose from the below list. Keep in mind, when running the Azure Functions locally, the authorization attribute is ignored, and you can call any function no matter which level is specified. These authorizations will works only after publishing the code in Azure. It can receive request data via query string parameters, request body data or URL route templates. Like other functions, they can also integrate with other Azure services such as Blob Storage, Event Hubs, queues and so on.

Authorization Types

  1. Function
  2. Anonymous
  3. Admin
  4. System
  5. User

 

Function

Function, Admin & System authorization levels are key based.

Anonymous

If you want a function to be accessed by anyone, the following piece of code will work, because the authorization is set to Anonymous.

Admin

Admin authorization level requires a host key for authorization. Passing a function key will fail authorization and return an HTTP 401 – Unauthorized error code.
 

Create an Azure function app in Visual Studio

In Visual Studio:

Create a project by selecting File > New > Project

How To Create An HTTP Trigger Azure Function App Using Visual Studio 2017

Select Visual C# > Cloud > Azure Functions

How To Create An HTTP Trigger Azure Function App Using Visual Studio 2017

Click Ok

Select HttpTrigger and Access rights > Admin,

How To Create An HTTP Trigger Azure Function App Using Visual Studio 2017

Input Data

  1. {  
  2.    "userName""maniteja@mani.com",  
  3.    "password""password",  
  4.    "Employees""{\r\n \"MethodName\": \"CheckMethod\",\r\n \"entities\": [],\r\n \"text\": \"testword\",\r\n \"isActive\": false\r\n}",  
  5.    "IsCheck""true"  
  6. }  

Now, I’ll create class file with name of CommonReturnType to read the input data.

How To Create An HTTP Trigger Azure Function App Using Visual Studio 2017

Now, open commonreturntype class file.

How To Create An HTTP Trigger Azure Function App Using Visual Studio 2017

and keep the JSON data content on the clipboardd (copy input JSON data).

How To Create An HTTP Trigger Azure Function App Using Visual Studio 2017

Go to Edit > Paste Special > Select Paste JSON as Classes.

How To Create An HTTP Trigger Azure Function App Using Visual Studio 2017

Then, an auto generated class will be retrieved as per the JSON data.

How To Create An HTTP Trigger Azure Function App Using Visual Studio 2017

Next

Develop the code as per our requirement. 

  1. using System.Linq;  
  2. using System.Net;  
  3. using System.Net.Http;  
  4. using System.Threading.Tasks;  
  5. using Microsoft.Azure.WebJobs;  
  6. using Microsoft.Azure.WebJobs.Extensions.Http;  
  7. using Microsoft.Azure.WebJobs.Host;  
  8. using Newtonsoft.Json;  
  9. namespace FunctionApp1 {  
  10.     public static class Function1 {  
  11.         [FunctionName("MyFirstFunction")]  
  12.         public static async Task < HttpResponseMessage > Run([HttpTrigger(AuthorizationLevel.Admin, "get""post", Route = null)] HttpRequestMessage req, TraceWriter log) {  
  13.             log.Info("C# HTTP trigger function processed a request.");  
  14.             string jsonContent = await req.Content.ReadAsStringAsync();  
  15.             if (jsonContent != "") {  
  16.                 /*De serailizing json data here*/  
  17.                 CommonReturnType fdata = JsonConvert.DeserializeObject < CommonReturnType > (jsonContent);  
  18.                 string inputMessageJSON = fdata.Employees;  
  19.                 Employees msg = JsonConvert.DeserializeObject < Employees > (inputMessageJSON);  
  20.                 /*De serailizing json data here*/  
  21.                 if (msg.MethodName == "CheckMethod" && fdata.IsCheck == "true") {  
  22.                     string swin = msg.text;  
  23.                     switch (swin.ToLower()) {  
  24.                         case "something":  
  25.                             msg.text = "How are you today";  
  26.                             fdata.IsCheck = "true";  
  27.                             break;  
  28.                         case "something1":  
  29.                             msg.text = "Good Bye";  
  30.                             fdata.IsCheck = "false";  
  31.                             break;  
  32.                         default:  
  33.                             msg.text = "Hi. How may I help you today..!";  
  34.                             fdata.IsCheck = "true";  
  35.                             break;  
  36.                     }  
  37.                     fdata.Employees = JsonConvert.SerializeObject(msg, Formatting.Indented);  
  38.                     return req.CreateResponse(HttpStatusCode.OK, fdata);  
  39.                 }  
  40.             }  
  41.             return req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body");  
  42.         }  
  43.     }  
  44. }  

Execute the code.

How To Create An HTTP Trigger Azure Function App Using Visual Studio 2017

And test the below URL in Postman App: http://localhost:7071/api/MyFirstFunction

How To Create An HTTP Trigger Azure Function App Using Visual Studio 2017

If you want to publish this Azure Function in the cloud, publish this code in the Azure cloud by using credentials and publish file.

How To Create An HTTP Trigger Azure Function App Using Visual Studio 2017
 

Azure URL Example

 
To execute this function app in Azure, you have to mention the secret key in the URL.
 
Example URL

http://mysite.azurewebsites.net/api/MyFirstFunction?code=MySecretCodehere

Happy coding.