json - 在Python中的azure函数应用程序中未在 `req.get_json()`中获取json输入

标签 json python-3.x azure azure-functions httprequest

我之前在azure函数应用程序中创建了一些api。我曾使用 req.get_json() 来获取 json 输入参数,但突然停止了。 req.get_json() 的值给我错误 ValueError: HTTP 请求不包含有效的 JSON 数据。我尝试了以下基本代码示例。它给了我同样的错误。

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

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
    else:
        return func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
             status_code=200
        )

curl 命令:

curl --location --request POST 'http://localhost:7071/api/dev-test' \
> --header 'Content-Type: application/json' \
> --data-raw '{"name":"test-dev"}​'

curl 的输出:

This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.

最佳答案

我以前遇到过同样的问题,我认为这主要是 postman 的问题。您可以下载postcode扩展名 vscode来自市场并测试使用它。

curl命令您可能还会遇到 Frank 提到的问题多于。要消除该问题,您可以创建一个 JSON文件并将 json 数据粘贴到其中,然后尝试使用curl,例如: curl --location --request POST "http://localhost:7071/api/dev-test" --header 'Content-Type: application/json' -d @test.json哪里test.json将包含您的 json 数据。它可能有效,也可能无效。

关于json - 在Python中的azure函数应用程序中未在 `req.get_json()`中获取json输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67919944/

相关文章:

php - Laravel 4 REST API 连接表

python - 如何通过字典进行测试

python - 如何在cygwin上通过Python脚本调用文件

python - 组合和制表几个文本 block

asp.net - 如何使用SSIS将放置在azure存储容器中的文件提取到本地计算机

javascript - JSON解析错误

php - 如何检索YouTube channel (PHP)的个人资料图片?

azure - 如何将 Azure VM 内的 IIS 中的网站设置为始终打开?

php - 使用 url 根据值过滤 json 结果

.net - 表单识别器 API |自 2020 年 5 月 11 日起修复 |客户端使用空字段替换结果