微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用Azure函数将URL和参数重定向到另一个HTTP ServerPy

如何解决使用Azure函数将URL和参数重定向到另一个HTTP ServerPy

我一直在尝试编写执行以下操作的Azure函数

客户端/浏览器向https://foo.azurewebsites.com/$A/$B发出请求。一个示例看起来像http://foo.azurewebsites.com/john/30vjs0-s9fjs-fnsne

$A =最多20个可能的词典词之一。

$B =未知字符串,但是这些字符串也需要传递到辅助服务器上。

我想将整个HTTP请求(理想情况下为GET和POST)重定向到另一个HTTP服务器(Az之外)。

我了解我需要为$A中的每一个单独创建函数(每个函数内部均具有有效的相同代码),以将其重定向到其他HTTP服务器。但是,我似乎无法弄清楚如何在Az Functions模块内的Python中正确解析URL。我认为,这是由于处理$ B中的未知变量导致的,因为我无法对它们进行硬编码,因为我无法预测它们。 Az Functions当前没有传递与/functionName参数不完全匹配的任何东西-但在我的用例中,总会有一个$B

因此https://foo.azurewebsites.com/john将转发到我的SEOcondary服务器,但是https://foo.azurewebsites.com/john/test.txt似乎已被Az丢弃。

我的要在GET和POST上转发的Python在这里(对$ A列表中的每个函数重复执行):我主要是在处理GET请求。

def main(req: func.HttpRequest) -> func.HttpResponse:
    if req.method == "GET":
        return get(req)
    if req.method == "POST":
        return post(req)

def post(req: func.HttpRequest) -> func.HttpResponse:
       
    header_dict = {}
    get_url = 'https://secondaryserver.com'
    for key,value in dict(req.headers).items():
        header_dict.update({key : value})
   
    post_data = req.get_body()
    request = urllib.request.Request(get_url,data=post_data,headers=header_dict)
    with urllib.request.urlopen(request) as response:
        html = response.read()
    return func.HttpResponse(html)


def get(req: func.HttpRequest) -> func.HttpResponse:
    suffix = req.url
    get_url = 'https://secondaryserver.com'
    final_url = get_url + suffix
    header_dict = {}
    print(final_url)
    for key,value in dict(req.headers).items():
        header_dict.update({key : value})
    request = urllib.request.Request(final_url,headers=header_dict)
    with urllib.request.urlopen(request) as response:
        html = response.read()
    return func.HttpResponse(html)

我还调查了Microsoft的这篇文章,尝试在function.json中尝试“ route”:{url},然后在Python init .py中引用它,但未成功(Az报告了将URL触发为%7BURL%7B

我想一个关键问题是'Az Functions实际上是否可能-在应用程序中的函数名称之后传递参数,并能够在Python代码中引用该参数?

感谢您可能提供的帮助。

解决方法

如果要将某些请求路径路由到同一功能,则可以在route中定义function.json。有关更多详细信息,请参阅here

例如。我想将https://{}/$A/$B路由到相同的函数。我们应该如下定义function.json

{
  "scriptFile": "__init__.py","bindings": [
    {
      "authLevel": "anonymous","type": "httpTrigger","direction": "in","name": "req","methods": ["get","post"],"route": "{url}/{url1}"
    },{
      "type": "http","direction": "out","name": "$return"
    }
  ]
}

测试

我的功能代码

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
   

    
    return func.HttpResponse(
             req.url,status_code=200
        )

enter image description here enter image description here

更新

如果要将请求转发到另一个HTTP服务器,则可以参考以下代码

def main(req: func.HttpRequest) -> func.HttpResponse:
    if req.method == "GET":
        # send you request
    if req.method == "POST":
         # send you request

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。