DZone
Cloud Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Cloud Zone > HTTP-Triggered Azure Functions

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.

Gunnar Peipman user avatar by
Gunnar Peipman
·
Apr. 05, 17 · Cloud Zone · Tutorial
Like (1)
Save
Tweet
7.16K Views

Join the DZone community and get the full member experience.

Join For Free

azure 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.

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.

azure-functions-cli-http-function

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 Requests Cloud

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • MEAN vs MERN Stack: Which One Is Better?
  • Python 101: Equality vs. Identity
  • Version Number Anti-Patterns
  • Artificial Intelligence (AI) And Its Assistance in Medical Diagnosis

Comments

Cloud Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo