Azure Functions is a serverless compute service that enables you to run code on-demand without having to explicitly provision or manage infrastructure. Use Azure Functions to run a script or piece of code in response to a variety of events.
This article will show you how to create a new Azure Function that will expose an HTTP endpoint which will accept a couple of Query String parameters from the HTTP Request, and then will output a calculated value based on the input parameters.
Here we want to create a Function that is triggered by an HTTP endpoint.
Step 1
Login to your Microsoft Azure portal, click on Create new resource and select Function App from Compute category. Enter the Name, what Subscription you want it to belong to, create a new Resource Group, choose the hosting plan as Consumption Plan, the Location where we want to host the same and leave the Storage account as default (you can customize the name or select an existing according to your choice). Wait for a few minutes for the resource to be created.
Step 2
Once the deployment is succeeded, open the resource and click the Plus (+) next to the Functions node underneath the Azure Function App. This will bring up an interface to initiate the creation of a new Function within the app.
Step 3
Click on the Custom function link which you can find at the bottom of the Get started quickly interface.
Step 4
Select the HttpTriggerWithParameters C# Template from the Choose a template interface. This will setup a new Function that can be triggered over an HTTP endpoint and accept Query String parameters sent within the HTTP Request. It will also be setup to output to the HTTP Response. Make sure you select the C# Version.
Step 5
Leave the Language as C#, give a Name or go with default, leave the Authorization level function as default and click on default which will create the new function based on the template. Now we have just created a new function that is triggered by an HTTP endpoint.
Step 6
Here, the default template function will accept a single Query String parameter with the name of the HTTP Request. Then the function prefixes the passed value with Hello and returns the result in the HTTP Response.
Step 7
Click on the Run button above the code to open the Test Pane view to test the functionality of this function.
Step 8
In the Test pane to the right of the code view, enter a value in the value field underneath Query next to the name parameter and set GET for the HTTP Method and click run again with the configured setting and parameter (If it is showing status code error as unauthorized, go to manage blade of the function and click on renew for the function key)
Now triggering of the Function ran successfully with an HTTP Status code of 200 OK showing result of the Function that was returned via the HTTP Response of the Function.
Step 9
Now we will modify the Azure Function to accept 2 numbers as parameters and then return the result of multiplying those 2 numbers together. In the Run method remove the string name parameter and replace the return statement with the following code snippet that multiplies the 2 parameters together (one and two), then returns the result in the output of the method to send it to the HTTP Response. Click Save to save the code file.
- var val1 = double.Parse(req.GetQueryNameValuePairs().FirstOrDefault(q => string.Compare(q.Key, "val1", true) == 0).Value);
- var val2 = double.Parse(req.GetQueryNameValuePairs().FirstOrDefault(q => string.Compare(q.Key, "val2", true) == 0).Value);
- var sum = val1 + val2;
- return req.CreateResponse(HttpStatusCode.OK, sum);
Step 10
In the pane to the right of the code view, click on the View files tab, open the function.json which holds the json configuration for the Function and replace the contents of the function.json file with the following code snippet
- {
- "bindings": [{
- "authLevel": "function",
- "name": "req",
- "type": "httpTrigger",
- "direction": "in",
- "methods": ["get"]
- }, {
- "name": "$return",
- "type": "http",
- "direction": "out"
- }],
- "disabled": false
- }
Step 11
Go back to the Test tab and modify the Query parameters by removing the name parameter and adding 2 parameters named val1 and val2 with numerical values.
Step 12
Click the Run with two values in the value field and validate the Output of the method and the result that is displayed