Lab 3: Create a function that's triggered by an HTTP request

Task 1: Create an HTTP-triggered function

  1. On the taskbar, select the Windows Terminal icon.
  2. 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
    
  3. 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.

  4. Close the currently running Windows Terminal application.

Task 2: Write HTTP-triggered function code

  1. On the Start screen, select the Visual Studio Code tile.
  2. On the File menu, select Open Folder.
  3. In the File Explorer window that opens, browse to $HOME\training-az204\Labs\02\Starter\func, and then select Select Folder.
  4. On the Explorer pane of the Visual Studio Code window, open the Echo.cs file.
  5. 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);
             }
         }
     }
    
  6. Delete all the content within the Echo.cs file.

  7. 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;
    
  8. Create a new public static class named Echo:

     public static class Echo
     { }
    
  9. 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
     { }
    
  10. 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)
     { }
    
  11. 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)
     { }
    
  12. 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)
     { }
    
  13. 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)
         { }
     }
    
  14. In the Run method, enter the following line of code to log a fixed message:

     logger.LogInformation("Received a request");
    
  15. Enter the following line of code to echo the body of the HTTP request as the HTTP response:

     return new OkObjectResult(request.Body);
    
  16. 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);
         }
     }
    
  17. Select Save to save your changes to the Echo.cs file.

Task 3: Test the HTTP-triggered function by using httprepl

  1. On the taskbar, select the Windows Terminal icon.
  2. 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
    
  3. 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.

  4. 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
    
  5. 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.

  6. From the tool's prompt, run the following command to browse to the relative api directory:

     cd api
    
  7. Run the following command to browse to the relative echo directory:

     cd echo
    
  8. 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
    
  9. 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
    
  10. 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"
    
  11. 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"}"
    
  12. Run the following command to exit the httprepl application:

     exit
    
  13. 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.

results matching ""

    No results matching ""