0
0
Azurecloud~5 mins

Functions with HTTP triggers in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want your code to run when someone visits a web address or sends a web request. Azure Functions with HTTP triggers let you do this easily without managing servers.
When you want to run a small piece of code in response to a web request without setting up a full web server
When you need to create a simple API endpoint for your app or website
When you want to process form submissions or webhooks from other services
When you want to quickly test or prototype a web service without complex infrastructure
When you want to run code on demand triggered by HTTP calls from other applications
Config File - function.json
function.json
{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": ["get", "post"]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}

This file tells Azure that the function should start when it receives an HTTP request.

authLevel: Controls who can call the function. 'function' means a key is needed.

type: 'httpTrigger' means the function starts on HTTP requests.

methods: Lists allowed HTTP methods like GET and POST.

name: Variable names for input request and output response.

Commands
Create a new Azure Functions project using Python runtime to hold your functions.
Terminal
func init MyHttpFunctionProj --worker-runtime python
Expected OutputExpected
Creating new project in current directory... Writing host.json Writing local.settings.json Writing requirements.txt Writing .gitignore Project created successfully.
--worker-runtime - Specifies the language runtime for the function app
Add a new function named HttpExample triggered by HTTP requests with function-level authorization.
Terminal
func new --name HttpExample --template "HTTP trigger" --authlevel function
Expected OutputExpected
Function created successfully. Writing function.json Writing __init__.py Writing sample.dat
--template - Selects the function template to use
--authlevel - Sets the authorization level for the HTTP trigger
Run the function app locally so you can test the HTTP trigger on your machine.
Terminal
func start
Expected OutputExpected
Functions: HttpExample: [GET,POST] http://localhost:7071/api/HttpExample For detailed output, run func with --verbose flag.
Send an HTTP GET request to the function with a query parameter to see the response.
Terminal
curl http://localhost:7071/api/HttpExample?name=Azure
Expected OutputExpected
{"message": "Hello, Azure"}
Key Concept

If you remember nothing else from this pattern, remember: Azure Functions with HTTP triggers let you run code instantly when someone sends a web request, without managing servers.

Common Mistakes
Not setting the correct authorization level in function.json or when creating the function
The function may reject requests or be open to everyone unexpectedly
Set authLevel to 'function' or 'anonymous' explicitly depending on your security needs
Forgetting to run 'func start' before testing the function locally
The function app is not running, so HTTP requests fail
Always start the function app with 'func start' to test locally
Using the wrong HTTP method (e.g., POST instead of GET) when calling the function
The function only listens to specified methods and will ignore others
Check the allowed methods in function.json and use one of them
Summary
Initialize a new Azure Functions project with 'func init' specifying the runtime.
Create a new HTTP-triggered function with 'func new' and set authorization level.
Run the function app locally using 'func start' to test your function.
Send HTTP requests to the function URL to trigger your code and get responses.