5

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

davidism
  • 121,510
  • 29
  • 395
  • 339
Adithya
  • 241
  • 3
  • 16

3 Answers3

8

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')

Duc Quang
  • 166
  • 2
  • 4
3

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")
JIJO JOSEPH
  • 349
  • 2
  • 7
1

It depends on if you are using Functions V1 or Functions V2. Note that neither is currently in General Availability: the Python language in V1 is considered experimental and will likely never go GA. The Python language worker for Functions V2 just went into private preview, and is currently in active development (see here). Overall, I highly recommend using Functions 2.0 for the best python support, and because it supports Python 3, while I believe that Functions V1 only supports Python 2.

With that said, the two versions both use the route parameter on the HTTP trigger function.json to configure the routes. To actually read the values, the two versions have very different approaches.

V1

You can set the custom route the same way you do for Node and C# in the function.json, by setting the "route" field of your HTTP Trigger binding. The route parameters are available as strings on the environment variables REQ_PARAMS_{route_param_name_as_upper_case}. So for your example, the route parameter values for name and id would be REQ_PARAMS_NAME and REQ_PARAMS_ID respectively.

V2

Again, you set the custom route in the function.json file the same way you do in all other languages. The route parameters are attached on the http request object as the property route_params, with the type of Mapping[str, str]

def main(req):
    name = req.route_params.get('name')
    return f'Hello, {name}!'
Connor McMahon
  • 1,318
  • 7
  • 15
  • Do you have an example of the function.json file; how to add route param in that function.json file? – Luniam Jul 11 '19 at 17:18
  • I clarified that a bit in the answer. The trick is to use the "route" param on the Http Trigger binding in the function.json. The link I provided in the answer should link to some examples of how to use that value. – Connor McMahon Jul 12 '19 at 19:36