如何在Python Azure函数中使用特殊字符?

huangapple go评论92阅读模式
英文:

How can I use special chars in Python Azure Function?

问题

我有一个Azure函数(Python),我需要在request.params中获取特殊字符。

我想要接受的URL是"http://localhost:7071/api/testApp?Status>3"。

我的Python代码如下:

def main(req: func.HttpRequest) -> func.HttpResponse:
    try:
        Metadata = MetadataOperations()
        result = Metadata.get_items(req.params)

这是我得到的:

如何在Python Azure函数中使用特殊字符?

当URL是"http://localhost:7071/api/testApp?Status=3"时,一切都正常工作。
如何在Python Azure函数中使用特殊字符?

为了接受'>'字符,我应该怎么做?

谢谢。

英文:

I have an Azure Function (Python) and I need to get special chars in the request.params.

The URL I want to accept is "http://localhost:7071/api/testApp?Status>3"

My Python code is below:

def main(req: func.HttpRequest) -> func.HttpResponse:
    try:
        Metadata = MetadataOperations()
        result = Metadata.get_items(req.params)

This is what I get:

如何在Python Azure函数中使用特殊字符?

When the URL is "http://localhost:7071/api/testApp?Status=3" everything is working fine.
如何在Python Azure函数中使用特殊字符?

What should I do in order to accept '>' chars?

Thanks.

答案1

得分: 1

以下是翻译好的部分:

>如何在Python Azure函数中使用特殊字符?

我同意@Klaus D.的看法,我在我的环境中重现了这个问题,以下是我的观察结果。

你不能直接在URL中使用**>**。但你可以通过以下方式使用:

__init__.py

import azure.functions as func
import urllib.parse

def main(req: func.HttpRequest) -> func.HttpResponse:
    try:
        decpar = {k1: urllib.parse.unquote(v1) for k1, v1 in req.params.items()}
        return func.HttpResponse(f"Received query parameters are/Sent Parameters are: {decpar}", status_code=200)
    except Exception as ee:
        return func.HttpResponse(f"Error: {str(ee)}", status_code=500)

http://localhost:7071/api/HttpTrigger1?Status=%3E3

如何在Python Azure函数中使用特殊字符?

你需要像这样提供:?Status=%3E3

http://localhost:7071/api/HttpTrigger1?Status=%3E3

输出:

如何在Python Azure函数中使用特殊字符?

如何在Python Azure函数中使用特殊字符?

这样你就可以使用**>**,但不能直接使用。

英文:

>How can I use special chars in Python Azure Function?

I do agree with @Klaus D. and I have reproduced in my environment and below are my observations.

You cannot directly use > it in URL. But you can in below way:

__init__.py

import azure.functions as func
import urllib.parse

def main(req: func.HttpRequest) -> func.HttpResponse:
    try:
        decpar = {k1: urllib.parse.unquote(v1) for k1, v1 in req.params.items()}
        return func.HttpResponse(f"Received query parameters are/Sent Parameters are: {decpar}", status_code=200)
    except Exception as ee:
        return func.HttpResponse(f"Error: {str(ee)}", status_code=500)

http://localhost:7071/api/HttpTrigger1?Status=%3E3

如何在Python Azure函数中使用特殊字符?

You need to give like this : ?Status=%3E3

http://localhost:7071/api/HttpTrigger1?Status=%3E3

Output:

如何在Python Azure函数中使用特殊字符?

如何在Python Azure函数中使用特殊字符?

This way you can use > but not directly.

huangapple
  • 本文由 发表于 2023年7月11日 03:22:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定