You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Azure Functions Python v2编程模型中设置仅监听HTTP POST请求

Azure Functions v2 Python模型配置HTTP触发仅接受POST请求

在v2 Python编程模型中,有两种方式可以配置HTTP触发函数仅监听POST请求:

方式一:使用装饰器(推荐)

v2模型推荐通过代码装饰器定义触发规则,替代v1中function.json的配置。只需在@app.route()装饰器中指定methods=["POST"]即可:

import azure.functions as func
app = func.FunctionApp()

@app.route(
    route="your-function-endpoint",
    auth_level=func.AuthLevel.FUNCTION,
    methods=["POST"]
)
def post_only_function(req: func.HttpRequest) -> func.HttpResponse:
    # 处理POST请求的逻辑
    return func.HttpResponse("POST请求已接收", status_code=200)

当收到非POST请求时,函数会自动返回405 Method Not Allowed状态码。

方式二:保留function.json配置(兼容模式)

如果需要沿用function.json的配置方式,v2模型依然支持,配置格式和v1完全一致:

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

不过这种方式需要确保函数代码与function.json配置匹配,v2模型更推荐使用装饰器将配置与代码整合。

内容的提问来源于stack exchange,提问作者Jeff G

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 21:12:41