如何在Azure Function v4中访问http状态

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

如何读取使用 Azure Functions v4 构建的 http 触发器函数的 http 状态?

v3 中的等效内容类似于

const statusCode = context.res.status; 上下文.log(

HTTP Status Code: ${statusCode}
);

我尝试过调试并尝试查找上下文变量中的状态,但我知道它不存在。我可以从文档(https://learn.microsoft.com/en-us/azure/azure-functions/functions-node-upgrade-v4?tabs=v3)中看到,您可以使用返回设置状态状态例如返回{状态:200};但我不知道如何简单地读取当前状态。

谢谢!

azure function azure-functions http-status-codes azure-http-trigger
1个回答
0
投票

如何读取使用 Azure Functions v4 构建的 http 触发器函数的 http 状态?

我使用运行时堆栈 Python 创建了一个 Http 触发函数。

代码:

import azure.functions as func
import logging

app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)

@app.route(route="http_trigger")
def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        response = func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully")
    else:
        response = func.HttpResponse(
            "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
            status_code=200
        )
    
    status_code = response.status_code
    logging.info(f'HTTP Status Code: {status_code}')
    
    return response

以上代码执行成功。检查下面:

enter image description here

本地输出状态:

enter image description here

我已成功将http触发功能部署到Azure门户中。检查下面:

enter image description here

我可以看到功能门户。

enter image description here

然后单击该函数,转到

code+test
,然后运行代码。运行成功,状态如下: 输出:

enter image description here

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