@Retention(value=RUNTIME) @Target(value=PARAMETER) public @interface HttpTrigger
The HttpTrigger annotation is applied to Azure functions that will be triggered by a call to the HTTP endpoint that the function is located at. The HttpTrigger annotation should be applied to a method parameter of one of the following types:
HttpRequestMessage<T>For example:
@FunctionName("hello")
public HttpResponseMessage<String> helloFunction(
@HttpTrigger(name = "req",
methods = {HttpMethod.GET},
authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request
) {
....
}
In this code snippet you will observe that we have a function annotated with
@FunctionName("hello"), which indicates that this function will be available at the
endpoint /api/hello. The name of the method itself, in this case helloFunction is
irrelevant for all intents and purposes related to Azure Functions. Note however that the method
return type is HttpResponseMessage, and that the first
argument into the function is an HttpRequestMessage with
generic type Optional<String>. This indicates that the body of the request will
potentially contain a String value.
Most important of all however is the @HttpTrigger annotation that has been applied to
this argument. In this annotation you'll note that it has been given a name, as well as told what
type of requests it supports (in this case, only HTTP GET requests), and that the
AuthorizationLevel is anonymous, allowing access to anyone who can call the endpoint.
The HttpTrigger can be further customised by providing a custom route(), which
allows for custom endpoints to be specified, and for these endpoints to be parameterized with
arguments being bound to arguments provided to the function at runtime.
The following example shows a Java function that looks for a name parameter either in the query string (HTTP GET) or the body (HTTP POST) of the HTTP request. Notice that the return value is used for the output binding, but a return value attribute isn't required.
@FunctionName("readHttpName")
public String readName(
@HttpTrigger(name = "req",
methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS)
final HttpRequestMessage<Optional<String>> request) {
String name = request.getBody().orElseGet(() -> request.getQueryParameters().get("name"));
return name == null ?
"Please pass a name on the query string or in the request body" :
"Hello " + name;
}
HttpRequestMessage,
HttpResponseMessage| Modifier and Type | Required Element and Description |
|---|---|
String |
name
The variable name used in function code for the request or request body.
|
| Modifier and Type | Optional Element and Description |
|---|---|
AuthorizationLevel |
authLevel
Determines what keys, if any, need to be present on the request in order to invoke the
function.
|
String |
dataType
Defines how Functions runtime should treat the parameter value.
|
com.microsoft.azure.functions.HttpMethod[] |
methods
An array of the HTTP methods to which the function responds.
|
String |
route
Defines the route template, controlling which request URLs your function will respond to.
|
public abstract String name
public abstract String dataType
Defines how Functions runtime should treat the parameter value. Possible values are:
public abstract String route
Defines the route template, controlling which request URLs your function will respond to. The
default value if no route is provided is the function name specified in the
FunctionName annotation, applied to each Azure Function.
By default when you create a function for an HTTP trigger, or WebHook, the function is
addressable with a route of the form
http://<yourapp>.azurewebsites.net/api/<funcname>. You can customize this
route using this route property. For example, a route of
"products/{category:alpha}/{id:int}" would mean that the function is now addressable
with the following route instead of the original route:
http://<yourapp>.azurewebsites.net/api/products/electronics/357, which allows the
function code to support two parameters in the address: category and id. By specifying the
route in this way, developers can then add the additional route arguments as arguments into the
function by using the BindingName annotation. For example:
@FunctionName("routeTest")
public HttpResponseMessage<String> routeTest(
@HttpTrigger(name = "req",
methods = {HttpMethod.GET},
authLevel = AuthorizationLevel.ANONYMOUS,
route = "products/{category:alpha}/{id:int}")
HttpRequestMessage<Optional<String>> request,
@BindingName("category") String category,
@BindingName("id") int id,
final ExecutionContext context
) {
....
context.getLogger().info("We have " + category + " with id " + id);
....
}
For more details on the route syntax, refer to the online documentation.
public abstract com.microsoft.azure.functions.HttpMethod[] methods
public abstract AuthorizationLevel authLevel
Determines what keys, if any, need to be present on the request in order to invoke the function. The authorization level can be one of the following values:
For more information, see the documentation about authorization keys.
AuthorizationLevel value representing the level required to access the
function.Copyright © 2022. All rights reserved.