FastAPI:如何将正文读取为任何有效的json? [英] FastAPI: how to read body as any valid json?

查看:121
本文介绍了FastAPI:如何将正文读取为任何有效的json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,不精通Python.

Sorry, not proficient in Python.

我还没有找到该用例的文档.我如何获取请求正文,确保其有效的Json(任何有效的json ,包括数字,字符串,布尔值和null,不仅是对象和数组),还需要获取实际的Json.使用pydantic强制Json具有特定的结构.

I haven't found the docs for that use case. How can I get the request body, ensure its a valid Json (any valid json, including numbers, string, booleans and nulls, not only objects and arrays) and get the actual Json. Using pydantic forces the Json to have a specific structure.

推荐答案

您几乎可以在 Request 对象

您可以使用 request.body()

from fastapi import Request, FastAPI

@app.post("/dummypath")
async def get_body(request: Request):
    return await request.json()

这篇关于FastAPI:如何将正文读取为任何有效的json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆