脚本宝典收集整理的这篇文章主要介绍了FastAPI(46)- JSONResponse,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
https://www.cnblogs.com/poloyy/p/15364635.html
路径操作函数返回一个 Pydantic Model
#!usr/bin/env python # -*- coding:utf-8 _*- """ # author: 小菠萝测试笔记 # blog: https://www.cnblogs.com/poloyy/ # time: 2021/10/3 3:26 下午 # file: 38_responses.py """ from typing import Optional import uvicorn from fastapi import FastAPI from fastapi.encoders import jsonable_encoder from fastapi.responses import JSONResponse from pydantic import BaseModel app = FastAPI() class Item(BaseModel): id: str name: str title: Optional[str] = None @app.post("/item") async def get_item(item: Item): # 打印看看传进来的数据是什么 print(item, type(item)) # 直接返回传进来的数据 return item if __name__ == '__main__': uvicorn.run(app="38_responses:app", reload=True, host="127.0.0.1", port=8080)
Response Header 的显示 content-type 是 JSON
id='string' name='string' title='string' <class '38_responses.Item'> INFO: 127.0.0.1:51856 - "POST /item HTTP/1.1" 200 OK
@app.post("/item") async def get_item(item: Item): return item
这样写也能返回 JSON 数据,是因为FastAPI 是自动帮忙做了转换的
等价写法如下
from fastapi.encoders import jsonable_encoder @app.post("/item") async def get_item(item: Item): json_body = jsonable_encoder(item) return JSONResponse(content=json_body)
@app.post("/item2") async def get_item(item: Item): json_body = jsonable_encoder(item) print(json_body, type(json_body)) return JSONResponse(content=json_body)
console 打印结果
{'id': 'string', 'name': 'string', 'title': 'string'} <class 'dict'> INFO: 127.0.0.1:52880 - "POST /item2 HTTP/1.1" 200 OK
@app.post("/item3") async def get_item(item: Item): return JSONResponse(content=item)
访问该接口就会报错
raise TypeError(f'Object of type {o.__class__.__name__} ' TypeError: Object of type Item is not JSON serializable
会调用 json.dumps() 方法
看到其实可以自定义 status_code、headers、media_type 哦
headers 后面再用单独的篇幅来讲
@app.post("/item2") async def get_item(item: Item): json_item = jsonable_encoder(item) return JSONResponse(content=json_item, status_code=status.HTTP_201_CREATED)
https://www.cnblogs.com/poloyy/p/15364635.html
路径操作函数返回一个 Pydantic Model
#!usr/bin/env python # -*- coding:utf-8 _*- """ # author: 小菠萝测试笔记 # blog: https://www.cnblogs.com/poloyy/ # time: 2021/10/3 3:26 下午 # file: 38_responses.py """ from typing import Optional import uvicorn from fastapi import FastAPI from fastapi.encoders import jsonable_encoder from fastapi.responses import JSONResponse from pydantic import BaseModel app = FastAPI() class Item(BaseModel): id: str name: str title: Optional[str] = None @app.post("/item") async def get_item(item: Item): # 打印看看传进来的数据是什么 print(item, type(item)) # 直接返回传进来的数据 return item if __name__ == '__main__': uvicorn.run(app="38_responses:app", reload=True, host="127.0.0.1", port=8080)
Response Header 的显示 content-type 是 JSON
id='string' name='string' title='string' <class '38_responses.Item'> INFO: 127.0.0.1:51856 - "POST /item HTTP/1.1" 200 OK
@app.post("/item") async def get_item(item: Item): return item
这样写也能返回 JSON 数据,是因为FastAPI 是自动帮忙做了转换的
等价写法如下
from fastapi.encoders import jsonable_encoder @app.post("/item") async def get_item(item: Item): json_body = jsonable_encoder(item) return JSONResponse(content=json_body)
@app.post("/item2") async def get_item(item: Item): json_body = jsonable_encoder(item) print(json_body, type(json_body)) return JSONResponse(content=json_body)
console 打印结果
{'id': 'string', 'name': 'string', 'title': 'string'} <class 'dict'> INFO: 127.0.0.1:52880 - "POST /item2 HTTP/1.1" 200 OK
@app.post("/item3") async def get_item(item: Item): return JSONResponse(content=item)
访问该接口就会报错
raise TypeError(f'Object of type {o.__class__.__name__} ' TypeError: Object of type Item is not JSON serializable
会调用 json.dumps() 方法
看到其实可以自定义 status_code、headers、media_type 哦
headers 后面再用单独的篇幅来讲
@app.post("/item2") async def get_item(item: Item): json_item = jsonable_encoder(item) return JSONResponse(content=json_item, status_code=status.HTTP_201_CREATED)
以上是脚本宝典为你收集整理的FastAPI(46)- JSONResponse全部内容,希望文章能够帮你解决FastAPI(46)- JSONResponse所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。