首页 > 解决方案 > azure python 函数在 vscode 本地运行,但不在 azure PAYG functon 应用程序中运行。httpRequest.headers["referer"] 有问题吗?

问题描述

我从 Azure 日志中收到以下错误消息:

Result: Failure Exception: KeyError: 'referer' Stack: File "/azure-functions-
host/workers/python/3.7/LINUX/X64/azure_functions_worker/dispatcher.py", line 372, in 
_handle__invocation_request self.__run_sync_func, invocation_id, fi.func, args) File 
"/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run result = self.fn(*self.args,
 **self.kwargs) File "/azure-functions 
host/workers/python/3.7/LINUX/X64/azure_functions_worker/dispatcher.py", line 548, in __run_sync_func 
return func(**params) File "/home/site/wwwroot/createCheckoutSession/__init__.py", line 18, in main 
logging.info(req.headers["referer"]) File "/azure-functions- 
host/workers/python/3.7/LINUX/X64/azure/functions/_http.py", line 27, in __getitem__ return 
self.__http_headers__[key.lower()]

我认为有问题的代码是:

logging.info(req.headers["referer"])
logging.info(os.environ["stripe_api_key"])
BASEURL = req.headers["referer"] + "#"

Azure 似乎正在努力从 req.headers 中提取引用。它适用于 VScode。在 Stripe 支付请求后,我使用引荐来源网址构建重定向 URL。这种重定向在 DEV、TEST 和 PROD 中明显不同。任何想法我做错了什么。

标签: pythonazureserverless

解决方案


根据你的描述,我用下面的代码做了一些测试:

import logging

import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info(req.headers["referer"])
    BASEURL = req.headers["referer"] + "#"

    return func.HttpResponse(
            BASEURL,
            status_code=200
    )

但无论是在本地云还是 Azure 云上,它都可以例外地工作:

在此处输入图像描述

那么,您能否请检查一下,如果您还有其他问题,请告诉我?


推荐阅读