The HTTP trigger for Azure Functions allows you to create serverless APIs that respond to HTTP requests. Input bindings enable your function to easily access data from various sources, including the incoming HTTP request itself. This section details how to access common HTTP request elements like query parameters, route parameters, and request body as input to your function.
The request body can be accessed as a string or deserialized into an object, depending on the contentType header.
By default, if the contentType is application/json, Azure Functions runtime will attempt to deserialize the body into the target type.
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.IO;
using System.Threading.Tasks;
public static class HttpTriggerExample
{
[FunctionName("HttpTriggerWithBody")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
string name = data?.name;
int? age = data?.age;
string responseMessage = string.IsNullOrEmpty(name)
? "Hello, world! Please pass a name in the request body."
: $"Hello, {name}! Your age is {age}.";
return new OkObjectResult(responseMessage);
}
}
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
const name = (req.body && req.body.name) || (req.query && req.query.name);
const age = (req.body && req.body.age) || (req.query && req.query.age);
let responseMessage;
if (name) {
responseMessage = `Hello, ${name}! Your age is ${age}.`;
} else {
responseMessage = "Hello, world! Please pass a name in the request body or query string.";
}
context.res = {
status: 200,
body: responseMessage
};
};
Query parameters are accessible directly from the HttpRequest object (in C#) or via req.query (in JavaScript).
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
public static class HttpTriggerExample
{
[FunctionName("HttpTriggerWithQuery")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string responseMessage = string.IsNullOrEmpty(name)
? "Hello, world! Please pass a name in the query string."
: $"Hello, {name}!";
return new OkObjectResult(responseMessage);
}
}
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
const name = req.query.name || (req.body && req.body.name);
let responseMessage;
if (name) {
responseMessage = `Hello, ${name}!`;
} else {
responseMessage = "Hello, world! Please pass a name in the query string or request body.";
}
context.res = {
status: 200,
body: responseMessage
};
};
When you define a Route in your trigger attribute, you can specify parameters that will be extracted from the URL path.
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
public static class HttpTriggerExample
{
[FunctionName("HttpTriggerWithRoute")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "users/{userId}")] HttpRequest req,
string userId, // This parameter will be bound from the route
ILogger log)
{
log.LogInformation($"C# HTTP trigger function processed a request for user ID: {userId}");
string responseMessage = $"Hello, User {userId}!";
return new OkObjectResult(responseMessage);
}
}
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
// The 'userId' parameter is automatically available in context.bindingData
const userId = context.bindingData.userId;
let responseMessage;
if (userId) {
responseMessage = `Hello, User ${userId}!`;
} else {
responseMessage = "Hello, world! User ID not found in route.";
}
context.res = {
status: 200,
body: responseMessage
};
};
context.bindingData, whereas in C#, they are directly available as method parameters if named correctly.
HTTP headers can be accessed through the HttpRequest object in C# or req.headers in JavaScript.
// Inside your C# function...
string contentType = req.Headers["Content-Type"];
log.LogInformation($"Content-Type: {contentType}");
// Inside your JavaScript function...
const contentType = req.headers["content-type"];
context.log(`Content-Type: ${contentType}`);