I've been working on my URL shortener project recently, but I took a few days away from it to start writing some JavaDoc for the Java APIs for Azure Functions. In doing so I learned about a cool piece of API that might not be readily apparent (although I hope it is now that I've written documentation!), and so in this blog post I wanted to quickly introduce the route
field on the @HttpTrigger
annotation.
By default, when you create a function for an HTTP trigger the function is addressable with a route of the form http://<yourapp>.azurewebsites.net/api/<functionname>
. This is fine in many cases, but in some cases you want your endpoint to be parameterised. For example, we've all seen URLs such as reddit.com/r/java
, where the java
can be replaced by any value (I hope I'm not shattering illusions for anyone by informing you that these all aren't separate HTML pages sitting on a server :-) ). Doing this with Azure Functions is trivial, with help from the route
property. For example, we could specify a route of products/{category:alpha}/{id:int}
, and this would mean that the function is now addressable at http://<yourapp>.azurewebsites.net/api/products/electronics/357
.
If the only benefit was that the path was parameterised, we wouldn't bother using this API, which is why the other half of the feature is the ability to use the @BindingName
annotation to bring in these arguments into the function. For example, here is a full method signature with route
and @BindingName
in use (apologies for the odd line wrapping required):
@FunctionName("routeTest")
public HttpResponseMessage<String> routeTest(
@HttpTrigger(name = "req",
methods = {"get"},
authLevel = AuthorizationLevel.ANONYMOUS,
route = "products/{category:alpha}/{id:int}") HttpRequestMessage<String> request,
@BindingName("category") String category,
@BindingName("id") int id,
final ExecutionContext context) {
...
context.getLogger().info("We have " + category + " with id " + id);
...
}
As can be seen here, we are receiving the category and id values from the URL endpoint and they are being provided to us as arguments into the function itself, where they are immediately usable. The route
string can be quite complex, as there are a number of configuration options available. Microsoft has published useful documentation on routing (just ignore the C# noise :-) ).
This has just been a quick blog post on the routing support in Azure Functions, because I thought it was neat. I will keep posting this short snippets as I discover API that delights me. As always though: playing with Azure Functions is really fun - I recommend all Java developers take it for a spin to see what they can achieve when they don't need to worry about all the underlying infrastructure. Even better, you can get started with the free tier where you have 1,000,000 free function calls a month, and go from there! If you want more inspiration, check out my series on URL shorteners, built using Java and Azure Functions.
Thoughts on “Creating custom routes in Azure Functions”