How to set up an Azure Function that listens only to HTTP POST using python model v2?

341 views Asked by At

I'm migrating my Azure Function App from the v1 to v2 Python Programming Model. The one thing I can't figure out is how to have my HTTP-triggered function to listen only to POST requests. Meaning it should not accept GET requests.

In the v1 model, you would specify the exhaustive list of HTTP "methods" in function.json:

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

What's the equivalent in the v2 model?

1

There are 1 answers

0
Jeff G On

You can send a tuple of HTTP methods as a property on the @app.route decorator:

@app.route(route="migrate-datasets", methods=("POST",))
def migrate(req: func.HttpRequest) -> func.HttpResponse:
    pass

Documentation for this decorator: https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook-trigger?tabs=python-v2%2Cisolated-process%2Cnodejs-v4%2Cfunctionsv2&pivots=programming-language-python#decorators