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.
Join the DZone community and get the full member experience.
Join For Freeazure functions supports functions that are invoked by http requests . it makes it easy to have some scripted functionalities in the cloud that we can invoke via simple http requests. this article shows how to build and run http-triggered azure functions using visual studio.
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.
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.
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).
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.
Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Using OpenAI Embeddings Search With SingleStoreDB
-
Integration Testing Tutorial: A Comprehensive Guide With Examples And Best Practices
-
4 Expert Tips for High Availability and Disaster Recovery of Your Cloud Deployment
-
Seven Steps To Deploy Kedro Pipelines on Amazon EMR
Comments