HTTP-Triggered Azure Functions

DZone 's Guide to

HTTP-Triggered Azure Functions

As we dive back into Azure Functions, we'll set up a simple function invoked by HTTP requests — complete with some Postman tests and basic security.

· Cloud Zone ·
Free Resource

Triggering a piece of functionality in the cloud with HTTP requests and getting back something useful sounds good if we don’t have to build another web application around the functionality. Azure Functions supports also functions that can be triggered using HTTP request.

To build Azure Functions stuff on Visual Studio you need Visual Studio Tools for Azure Functions.

Default HTTP Function

When we create new HTTP functions using Visual Studio, we get a simple default function that is ready to for deployment on the cloud. It takes the HTTP parameter “name” as the input and based on whether it has a value, it says hello or asks for a name to be added with the net request. Here's the default HTTP function's code:

using System.Net; 

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
    log.Info($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}");// parse query parameter
    string name = req.GetQueryNameValuePairs()
        .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
        .Value;       // Get request body
    dynamic data = await req.Content.ReadAsAsync<object>();// Set name to query string or body data
    name = name ?? data?.name;       return name == null
        ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
        : req.CreateResponse(HttpStatusCode.OK, "Hello " + name);

To me, this code looks a little ugly, but it’s short and readable.

Running HTTP Functions on Visual Studio

We can run this function easily on Visual Studio if we have Visual Studio Tools for Azure Functions installed. Here is the screenshot of the Azure Functions CLI running the default HTTP function.

Azure Functions CLI: Running HTTP function

From the console, we can see that the URL of HTTP function is http://localhost:7071/api/HttpTriggerCSharp. We can use the browser to open this link and see the output. The screenshot below shows how the default HTTP function says hello to me.


Running HTTP Functions in the Cloud

When we publish the function to Azure, we cannot only use its URL to activate it. Everyone who knows the URL or who is able to guess it may run it. To avoid such problems, we have keys for running functions in the cloud. These keys are available at the Azure Portal and they are given to functions with request headers or parameters.

Here is an example of a request to the HTTP function published to the cloud. We are using the powerful Postman application for Google Chrome to play with requests. Notice that there is now an additional URL parameter called “code”. This is the function key mentioned above.

Request to HTTP function using Postman

Postman is powerful tool. It is the ideal choice if you need to test something by making tweaked HTTP requests. It is able to keep a request history, so you don’t have to start from zero again when opening it the next day.

Content Negotiation

HTTP functions on Azure also support content negotiation. It means that we can specify the expected output format. The example above used text/XML as the expected format. We can also change it to JSON or plain text. Here’s the example for plain text (see the Accept header in the screenshot).

Content negotiation: HTTP function returning JSON

If the output format is not known, then by default, JSON is returned.

Wrapping Up

Although the default HTTP function we get with the Visual Studio Azure Functions project is primitive and simple, it is still a good starting point for working with real code. Using Visual Studio Tools for Azure Functions, we can run functions on our local box without any need to publish it to the cloud. Functions can be published to the cloud using Visual Studio. When running on the cloud, we need to add a function key to the HTTP requests as a security measure. Otherwise, everybody could run our function. Using Postman for Google Chrome, we (sort of) have an HTTP request studio, which makes it easy for us to invoke functions and save them. HTTP functions are flexible – they can return data in JSON, XML, and plain text.

azure functions, cloud, http requests, tutorial, visual studio

Published at DZone with permission of Gunnar Peipman , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}