• FastAPI 学习之路(九)请求体有多个参数如何处理?


    请求体有多个参数如何处理?

     别的不多说,我们先写一个需求,然后演示下如何展示。

    需求:写一个接口,传递以下参数,书本的名称,描述,价格,打折。

    接口返回返回最后的价格

    我们去看下代码如何实现

    1. from fastapi import FastAPI
    2. from typing import Optional
    3. from pydantic import BaseModel
    4. app = FastAPI()
    5. class Item(BaseModel):
    6. name: str
    7. deacription: Optional[str] = None
    8. price: float
    9. tax: Optional[float] = None
    10. @app.put("/items")
    11. def update_item(item: Optional[Item]):
    12. result = {}
    13. if item.tax is not None:
    14. total = item.price * item.tax
    15. result["price"] = total
    16. result["name"] = item.name
    17. return result
    18. result["price"] = item.price
    19. result["name"] = item.name
    20. return result

    那么我们测试下,最后是否实现了这个功能,当我们输入所有的参数的时候。

    最后是在我们实际的打折上返回的。

              那么我们看下,我们不增加打折如何返回

    没有打折就原价返回了名称和价格。

    如果默认给了None或者其他内容,这个参数就是可以选择增加或者不增加。但是没有给默认值的时候,就是必须传递的,否则会返回对应的错误,我们可以看下。假如我们不传递价格。

    我们可以看到没有默认值的参数就是一个必须的。不然接口会返回对应的错误。

    除了声明以上单个的,我们还可以声明多个请求体参数,比如我们可以在之前的需求,增加一个返回,要求返回作者,和作者的朝代。如何实现呢。

    1. from fastapi import FastAPI
    2. from typing import Optional
    3. from pydantic import BaseModel
    4. app = FastAPI()
    5. class Item(BaseModel):
    6. name: str
    7. deacription: Optional[str] = None
    8. price: float
    9. tax: Optional[float] = None
    10. class User(BaseModel):
    11. username: str
    12. year: str
    13. @app.put("/items")
    14. def update_item(item: Optional[Item], user: User):
    15. result = {}
    16. if item.tax is not None:
    17. total = item.price * item.tax
    18. result["price"] = total
    19. result["name"] = item.name
    20. result["user"] = user
    21. return result
    22. result["price"] = item.price
    23. result["name"] = item.name
    24. result["user"] = user
    25. return result

    那么我们看下接口的请求

    当我们增加打折,看下返回结果: 

     我们可以看下接口的返回:

             FastAPI 将自动对请求中的数据进行转换,因此 item 参数将接收指定的内容,user 参数也是如此。

    我们要想在增加一个键,在哪里出售,但是要作为请求体的另一个键进行处理,如何 实现呢?

    1. from fastapi import FastAPI, Body
    2. from typing import Optional
    3. from pydantic import BaseModel
    4. app = FastAPI()
    5. class Item(BaseModel):
    6. name: str
    7. deacription: Optional[str] = None
    8. price: float
    9. tax: Optional[float] = None
    10. class User(BaseModel):
    11. username: str
    12. year: str
    13. @app.put("/items")
    14. def update_item(item: Optional[Item], user: User, sell: str = Body(...)):
    15. result = {}
    16. if item.tax is not None:
    17. total = item.price * item.tax
    18. result["price"] = total
    19. result["name"] = item.name
    20. result["user"] = user
    21. result["sell"] = sell
    22. return result
    23. result["price"] = item.price
    24. result["name"] = item.name
    25. result["user"] = user
    26. result["sell"] = sell
    27. return result

    我们可以看到如下

    假如我们把参数放在查询内,返回错误

    sell参数必须放在body内请求,因为指定了Body。

  • 相关阅读:
    Web3社交=身份系统(DID)+数据系统
    ⑱霍兰德ER*如何选专业?高考志愿填报选专业
    计算机学院改考后,网络空间安全学院也改考了!南京理工大学计算机考研
    在linux服务器运行python脚本异常ModuleNotFoundError: No module named '_ssl'
    LVS负载群集--DR模式
    【ATT&CK】ATT&CK视角下的水坑钓鱼攻防战法
    双亲委派模型机制
    解决Drag and drop is not supported导致无法将物理机上的文件拖入Ubuntu
    面试经典150题——生命游戏
    如何在已有的vue项目里面使用electron? (普通项目转成桌面端应用)
  • 原文地址:https://blog.csdn.net/myli_binbin/article/details/126538457