How to Use HTTP Trigger Azure Function: Simple Guide
To use an
HTTP trigger Azure Function, create a function that runs when it receives an HTTP request. You write your code to handle the request and return a response. This lets you build APIs or webhooks easily with Azure Functions.Syntax
An HTTP trigger Azure Function listens for HTTP requests and runs your code when a request arrives. It has these parts:
- Function signature: Defines the HTTP request and response objects.
- Trigger type: Set to
httpTriggerto listen for HTTP calls. - Authorization level: Controls who can call the function (e.g., anonymous or function key required).
javascript
module.exports = async function (context, req) { context.log('HTTP trigger function processed a request.'); const name = (req.query.name || (req.body && req.body.name)); const responseMessage = name ? `Hello, ${name}. This HTTP triggered function executed successfully.` : "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."; context.res = { status: 200, body: responseMessage }; };
Example
This example shows a simple HTTP trigger Azure Function in JavaScript. It reads a name from the query or body and returns a greeting message.
javascript
module.exports = async function (context, req) { context.log('HTTP trigger function processed a request.'); const name = (req.query.name || (req.body && req.body.name)); const responseMessage = name ? `Hello, ${name}. This HTTP triggered function executed successfully.` : "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."; context.res = { status: 200, body: responseMessage }; };
Output
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Hello, Alice. This HTTP triggered function executed successfully.
Common Pitfalls
Common mistakes when using HTTP trigger Azure Functions include:
- Not setting the correct
authLevelinfunction.json, causing unauthorized errors. - Forgetting to return a response, which leads to timeouts.
- Not parsing JSON body correctly when expecting POST data.
- Using synchronous code without
async/await, causing unexpected behavior.
javascript
/* Wrong: No response set, function will timeout */ module.exports = async function (context, req) { context.log('No response set here'); }; /* Right: Always set context.res with status and body */ module.exports = async function (context, req) { context.res = { status: 200, body: "Response sent correctly" }; };
Quick Reference
Remember these tips when using HTTP trigger Azure Functions:
- Use
context.reqto access request data. - Set
context.resto send the HTTP response. - Choose
authLevelwisely:anonymousfor open access,functionfor key protection. - Use
async/awaitfor asynchronous operations.
Key Takeaways
HTTP trigger Azure Functions run code when they receive HTTP requests.
Always set the response using context.res to avoid timeouts.
Use async functions and parse request data carefully.
Set the correct authorization level to control access.
You can read query parameters or JSON body from the request.