• 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。

  • 相关阅读:
    AI新能量!FortiGate NGFW面向数据中心全面集成FortiGuard AI 安全服务
    2023年中国背光显示面板分类、市场规模及企业分析[图]
    美容院沙龙活动方案
    node插件MongoDB(二)——MongoDB的基本命令
    LeetCode #2.两数相加
    机器学习笔记之EM算法(三)隐变量与EM算法的本质
    【模拟电路建模与控制系统分析】01Laplace变换
    RedisJson 横空出世!
    计算机组成原理_Cache写策略
    自学数据库- MongoDB
  • 原文地址:https://blog.csdn.net/myli_binbin/article/details/126538457