是否可以从函数应用程序中的 Azure API 网关调用中检索 headers?

Is it possible to retrieve the headers from an Azure API Gateway call in a Function App?

假设我有一个函数,我想打印所有传递给 API 网关调用的 headers。这可能吗?

Http触发功能App代码如下:

import logging
import json

import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    return func.HttpResponse(
        json.dumps(req.get_json()),
        status_code=200
    )

没有看到从 HttpRequest class 中检索它的任何明显方法:https://docs.microsoft.com/en-us/python/api/azure-functions/azure.functions.http.httprequest?view=azure-python

是的,只需使用“headers”字段:

import logging
import json

import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    for header in req.headers:
        logging.info(f'HEADER KEY {header}')
        logging.info(f'HEADER VALUE {req.headers[header]}')
    return func.HttpResponse(
        "hello",
        status_code=200
    )

您可以简单地将 func.HttpRequest.headers 转换为字典:

def main(req: func.HttpRequest) -> func.HttpResponse:
    headersAsDict = dict(req.headers)
    logging.info(json.dumps(headersAsDict, indent=2))
    ...