Azure Functions: Proxies
Azure Functions Proxies has just been announced as preview a couple of days ago and I played with it for a little while. It really fills the gap for a lot of people.
First of all, if you haven't played with proxies yet, go to https://docs.microsoft.com/en-us/azure/azure-functions/functions-proxies to get started, then come back here :D and...
Let's take some simple scenarios:
Simplifying URL
I've got a simple Azure Function and the full URL to call it is https://demotspfunction.azurewebsites.net/api/SimpleHttpGetCall/?name=test
and I want to tidy the URL a bit by calling https://demotspfunction.azurewebsites.net/simple
.
If I want a parameter to come after simple/
, I can tweak it like this:
Route template: simple/{name}
Backend URL: https://demotspfunction.azurewebsites.net/api/SimpleHttpGetCall/?name={name}
If I just want to take the rest of the URL and pass it over to the backend URL I simply put a star in front of the last parameter in the route template:
Route template: simple/{*rest}
Backend URL: https://demotspfunction.azurewebsites.net/api/SimpleHttpGetCall/{rest}
After publishing this post and tweeting it, I got a response from Anthony Chu about being able to achieve the same thing without using proxies. You can set the
methods
androute
properties in your function.json, like below:
{
"authLevel": "anonymous",
"name": "req",
"type": "httpTrigger",
"direction": "in",
"methods": [
"GET"
],
"route": "simple/{name}"
}
Routing External Resources
You can also route external resources, it doesn't need to be an Azure Function at all and I've seen people talking a lot about it. For example in some cases where you don't have control over CORS (Cross-origin resource sharing), you might want to create a proxy in your Function App and configure the allowed origins in the app settings.
Using the Same URL for different HTTP methods
Let's take a simple product api as an example, here are some basic methods to cover. Each of the methods will be configured as a separate proxy.
[GET] /product
Allowed HTTP methods: GET
Route template: product
Backend URL: https://demotspfunction.azurewebsites.net/api/GetProducts
[GET] /product/{id}
Allowed HTTP methods: GET
Route template: product/{id}
Backend URL: https://demotspfunction.azurewebsites.net/api/GetProduct?id={id}
[POST] /product
Allowed HTTP methods: POST
Route template: product
Backend URL: https://demotspfunction.azurewebsites.net/api/PostProduct
[DELETE] /product/{id}
Allowed HTTP methods: DELETE
Route template: product/{id}
Backend URL: https://demotspfunction.azurewebsites.net/api/DeleteProduct?id={id}
Same of Simplifying URL applies to this section. Thanks to Anthony Chu
Something I'd Love to see implemented
Creating multiple proxies for a single api might be a little time consuming, especially if you have multiple entities. I'd love to be able to grab the HTTP method and pass it over to the backend URL and play a bit more of the route template, for example:
Route template: {entity}
Allowed HTTP methods: ALL
Backend URL: https://{MY FUNCTION APP URL}/api/{HTTP METHOD}{entity}
I'll come back to this post if some other ideas come in mind or if this feature is implemented.
Cheers.