Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routing in Azure Functions using python

I know that I can use query parameters in Azure functions to get the values

"myfunction?p=one&p2=two"

I am referring to this question

How can I do Routing in Azure Functions?

However it only addresses C# and node.js, I want to get the values following flask style

/function/<name>/<id>

which I can directly access, how do I do it in python in Azure functions

I also referred this doc, which only talks about node.js and C#

https://github.com/Azure/azure-functions-host/wiki/Http-Functions

like image 463
Adithya Avatar asked May 12 '26 18:05

Adithya


2 Answers

You can add "route" to the function.json file to change the path, for example:

"bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "post"
      ],
      "route": "contact/{version}/certificate"
    }
    ....
    ]

And in file __init__.py, you can get version by version = req.route_params.get('version')

like image 88
Duc Quang Avatar answered May 15 '26 08:05

Duc Quang


To be more dynamic like the path type of Python Flask route in azure functions you can customize the route to be like below. Main Concept is in the route's *{restOfPath}

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ],
      "route": "Function1/{*restOfPath}"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
  ]
}

and edit the host.json to remove the word api from the URL like below.

{
  "http": {
    "routePrefix": ""
  }
}

so with above configuration you can have the Function to serve URLs like below

  1. http://localhost:7071/Function1
  2. http://localhost:7071/Function1/name
  3. http://localhost:7071/Function1/name/id
  4. http://localhost:7071/Function1/name/id/sub-name
  5. http://localhost:7071/Function1/name/id/sub-name/sub-id etc ...

Again to access it in init.py use it like below. You would get it as string

req.route_params.get("restOfPath")
like image 32
JIJO JOSEPH Avatar answered May 15 '26 07:05

JIJO JOSEPH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!