Lab 3: Create a function that's triggered by an HTTP request
Task 1: Create an HTTP-triggered function
- On the taskbar, select the Windows Terminal icon.
Run the following command to change the current directory to the $HOME\training-az204\Labs\02\Starter\func directory:
cd $HOME\training-az204\Labs\02\Starter\func
Run the following command to use the Azure Functions Core Tools to create a new function named Echo using the HTTP trigger template:
func new --template "HTTP trigger" --name "Echo"
Note: You can review the documentation to [create a new function][azure-functions-core-tools-new-function] using the Azure Functions Core Tools.
Close the currently running Windows Terminal application.
Task 2: Write HTTP-triggered function code
- On the Start screen, select the Visual Studio Code tile.
- On the File menu, select Open Folder.
- In the File Explorer window that opens, browse to $HOME\training-az204\Labs\02\Starter\func, and then select Select Folder.
- On the Explorer pane of the Visual Studio Code window, open the Echo.cs file.
In the code editor, observe the example implementation:
using System; using System.IO; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Extensions.Http; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Newtonsoft.Json; namespace func { public static class Echo { [FunctionName("Echo")] public static async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string name = req.Query["name"]; string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); name = name ?? data?.name; string responseMessage = string.IsNullOrEmpty(name) ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response." : $"Hello, {name}. This HTTP triggered function executed successfully."; return new OkObjectResult(responseMessage); } } }
Delete all the content within the Echo.cs file.
Add the following lines of code to add using directives for the Microsoft.AspNetCore.Mvc, Microsoft.Azure.WebJobs, Microsoft.AspNetCore.Http, and Microsoft.Extensions.Logging namespaces:
using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.WebJobs; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging;
Create a new public static class named Echo:
public static class Echo { }
Observe the Echo.cs file again, which should now include:
using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.WebJobs; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; public static class Echo { }
Within the Echo class, add the following code block to create a new public static method named Run that returns a variable of type IActionResult and that also takes in variables of type HttpRequest and ILogger as parameters named request and logger:
public static IActionResult Run( HttpRequest request, ILogger logger) { }
Add the following code to append an attribute to the Run method of type FunctionNameAttribute that has its name parameter set to a value of Echo:
[FunctionName("Echo")] public static IActionResult Run( HttpRequest request, ILogger logger) { }
Add the following code to append an attribute to the request parameter of type HttpTriggerAttribute that has its methods parameter array set to a single value of POST:
[FunctionName("Echo")] public static IActionResult Run( [HttpTrigger("POST")] HttpRequest request, ILogger logger) { }
Observe the Echo.cs file again, which should now include the following code:
using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.WebJobs; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; public static class Echo { [FunctionName("Echo")] public static IActionResult Run( [HttpTrigger("POST")] HttpRequest request, ILogger logger) { } }
In the Run method, enter the following line of code to log a fixed message:
logger.LogInformation("Received a request");
Enter the following line of code to echo the body of the HTTP request as the HTTP response:
return new OkObjectResult(request.Body);
Observe the Echo.cs file again, which should now include the following code:
using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.WebJobs; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; public static class Echo { [FunctionName("Echo")] public static IActionResult Run( [HttpTrigger("POST")] HttpRequest request, ILogger logger) { logger.LogInformation("Received a request"); return new OkObjectResult(request.Body); } }
Select Save to save your changes to the Echo.cs file.
Task 3: Test the HTTP-triggered function by using httprepl
- On the taskbar, select the Windows Terminal icon.
Run the following command to change the current directory to the $HOME\training-az204\Labs\02\Starter\func directory:
cd $HOME\training-az204\Labs\02\Starter\func
Run the following command to run the function app project:
func start --build
Note: You can review the documentation to start the function app project locally using the Azure Functions Core Tools.
On the taskbar, select the Windows Terminal icon again to open a new instance of the application. Run the following command to change the current directory to the $HOME\training-az204\Labs\02\Starter\func empty directory:
cd $HOME\training-az204\Labs\02\Starter\func
From the command prompt, run the following command to install and start the httprepl tool, setting the base Uniform Resource Identifier (URI) to
http://localhost:7071
:dotnet tool install -g Microsoft.dotnet-httprepl httprepl http://localhost:7071
Note: An error message is displayed by the httprepl tool. This message occurs because the tool is searching for a Swagger definition file to use to traverse the API. Because your function project doesn't produce a Swagger definition file, you'll need to traverse the API manually.
From the tool's prompt, run the following command to browse to the relative api directory:
cd api
Run the following command to browse to the relative echo directory:
cd echo
Run the following command to run the post command, sending in an HTTP request body set to a numeric value of 3 by using the --content option:
post --content 3
Run the following command to run the post command, sending in an HTTP request body set to a numeric value of 5 by using the --content option:
post --content 5
Run the following command to run the post command, sending in an HTTP request body set to a string value of Hello by using the --content option:
post --content "Hello"
Run the following command to run the post command, sending in an HTTP request body set to a JavaScript Object Notation (JSON) value of {"msg": "Successful"} by using the --content option:
post --content "{"msg": "Successful"}"
Run the following command to exit the httprepl application:
exit
Close all currently running instances of the Windows Terminal application.
Review
In this exercise, you created a basic function that echoes the content sent through an HTTP POST request.