Search code examples
pythonazure-functions

Get route within azure function and python


I'm searching for a way to get the route of my azure-function from the app or req object. I assume this should be straight forward. But I'm not able to find it.

 import azure.functions as func
 
 app = func.Function(...)
 
 app.route(route="myfunction")
 def my_function(req: func.Httprequest) -> func.HttpResponse:
    route = ??    # should be myfunction

Does any one know how to get the route?

Edit

When I inspect the route_params of req in debugg mode I see this:

  req.route_params.values()
  > dict_values([])
  req.route_params.keys()
  > dict_keys([])

Solution

  • Get route within azure function and python

    You can use below code:

    import azure.functions as func
    import logging
    
    app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
    
    @app.route(route="rithwik")
    def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
        logging.info('Hello!!')
        rith = req.url.split('/')[-1] 
        logging.info(f"Route: {rith}")
        return func.HttpResponse(f"Hello, route value is {rith}.")
    

    Output:

    enter image description here

    enter image description here

    If there is many / in route then you can use code like below:

    import azure.functions as func
    import logging
    from urllib.parse import urlparse
    
    app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
    
    @app.route(route="rithwik/r")
    def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
        logging.info('Hello!!')
        x = urlparse(req.url)
        rith =x.path.split('/api/', 1)[-1]
        logging.info(f"Route: {rith}")
        return func.HttpResponse(f"Hello, route value is {rith}.")
    

    Output:

    enter image description here

    enter image description here