我之前在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/