0

我从 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 中明显不同。任何想法我做错了什么。

4

2 回答 2

0

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

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 云上,它都可以例外地工作:

在此处输入图像描述

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

于 2021-04-08T02:53:03.050 回答
0

追了几天尾巴,我终于发现答案正是错误所说的。

该函数是从 azurestaticapps.net 部署中调用的,默认情况下这不会发送引用者

从 DEV 中提取标头:

Host: localhost:7071
Origin: http://localhost:8080
Referer: http://localhost:8080/

从 staticapps.net 中提取标题

Host: jumbletrailuk.azurewebsites.net
Origin: https://blue-plant-HiddenForSecurity.azurestaticapps.net
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"

但是,当在确实使用 Referer 的 Azure Functions 门户中运行/测试它时,它也没有在那里失败。

于 2021-05-07T11:28:20.073 回答