从 webhook 接收帖子时出现 Azure 函数应用程序异常错误

问题描述 投票:0回答:1

我将以下内容作为天蓝色函数应用程序的入口点,当我打开邮递员并向此函数应用程序发送帖子时,我收到此错误。

[错误]执行了“Functions.http_trigger_from_vrli”(失败,

当我在本地加载此功能应用程序时,该错误没有任何用处,一切正常,我可以使用正文中的发布数据向 https://localhost:7071/api/blahpost 发布帖子。代码在 updateRT 上执行,所有后续功能均成功完成。只是当我尝试使用 azure 函数应用程序作为 URL 时,我收到 500 和上述错误。我是否在路由点上做错了什么,导致每当收到有效负载时都会将其变为 500?我可以通过记录器确认我已经通过了 if 语句,它正在打印客户端 IP 地址,但似乎没有执行 if 语句内的函数。我通过 cd'ing 到 vscode 中的项目进行调试,然后在本地运行它并发送帖子时再次运行“func start --verbose”,它按预期工作。

这是有效负载的示例

{ "alert_type": "RATE_BASED_WITH_GROUPS", "alert_name": "阻止我", "search_period": "300000", "hit_oeprator": "GREATER_THAN", "messages": "[{"fields":[{"name":"msg","content":"主凭据被拒绝 - 无效用户"},{"name":"count","content":"150 "},{"name":"client_ip","content":"178.18.193.158"},{"name":"用户名","content":"stuff"},{"name":"status", "内容":"拒绝"}],"计数":150.0}]" }

这也是我在函数应用程序的根目录运行的内容

import azure.functions as func
import logging
import updateRT
app = func.FunctionApp(blah=func.AuthLevel.FUNCTION)
@app.route(route="blahpost")
def blahpost(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    try:
        req_body = req.get_json()
    except ValueError:
        return func.HttpResponse(
             "Invalid JSON format",
             status_code=400
        )
    alert_name = req_body.get('alert_name')
    messages = req_body.get('messages')
    if alert_name == "BlockMe":
        logging.info(req_body)
        for item in messages['fields']:
            if item['name'] == 'client_ip':
                targetIP = item['content']
                logging.info(targetIP)
                response = updateRT.main(targetIP)
                logging.info(response)
                return func.HttpResponse(response, status_code=200)
    if alert_name == "USER-Lockout-Alert":
        logging.info(req_body)
        for item in messages['fields']:
            if item['name'] == 'username':
                username = item['content']
                logging.info(username)
    return func.HttpResponse("Function executed successfully.")
python-3.x azure-functions
1个回答
0
投票

Live Metric 为我解决了这个问题,我的问题是 2 倍 json 输出不是我所期望的,将其添加到 for 循环修复了 json 问题

for item in messages[0]['fields']:

查看实时指标,我可以看到我为调用脚本创建的记录器出错,注释了 createLogger() 函数,并告诉 main 使用此函数在函数应用程序中使用记录器。

logger = logging.getLogger(__name__)

这两项更改之后,一切都按预期进行。

© www.soinside.com 2019 - 2024. All rights reserved.