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

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

系列文章:

  FastAPI 学习之路(一)fastapi--高性能web开发框架

  FastAPI 学习之路(二)

  FastAPI 学习之路(三)

  FastAPI 学习之路(四)

  FastAPI 学习之路(五)

     FastAPI 学习之路(六)查询参数,字符串的校验

  FastAPI 学习之路(七)字符串的校验

  FastAPI 学习之路(八)路径参数和数值的校验


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


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


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


接口返回返回最后的价格


我们去看下代码如何实现


from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: Optional[float] = None
@app.put("/items")
def update_item(item: Optional[Item]):
    result={}
    if  item.tax is not None:
        total=item.price*item.tax
        result['price'] = total
        result['name']=item.name
        return result
    result['price'] = item.price
    result['name'] = item.name
    return result


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


image.png


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


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


image.png


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


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


image.png


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

       

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


from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: Optional[float] = None
class User(BaseModel):
    username: str
    year: str
@app.put("/items")
async def update_item(item: Optional[Item],user:User):
    result={}
    if  item.tax is not None:
        total=item.price*item.tax
        result['price'] = total
        result['name']=item.name
        result['user']=user
        return result
    result['price'] = item.price
    result['name'] = item.name
    result['user']=user
    return result


那么我们看下接口的请求


image.png


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


image.png


 

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


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


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


from typing import Optional
from fastapi import FastAPI,Body
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: Optional[float] = None
class User(BaseModel):
    username: str
    year: str
@app.put("/items")
async def update_item(item: Optional[Item],user:User,sell:str=Body(...)):
    result={}
    if  item.tax is not None:
        total=item.price*item.tax
        result['price'] = total
        result['name']=item.name
        result['user']=user
        result['sell']=sell
        return result
    result['price'] = item.price
    result['name'] = item.name
    result['user']=user
    result['sell'] = sell
    return result


我们可以看到如下


image.png


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


image.png


参数必须放在body内请求。


相关文章
|
9月前
|
数据采集 JSON JavaScript
全面拥抱FastApi —三大参数及验证
全面拥抱FastApi —三大参数及验证
|
4天前
|
JSON API 数据安全/隐私保护
如何使用Fastapi上传文件?先从请求体数据讲起
如何使用Fastapi上传文件?先从请求体数据讲起
|
4天前
|
Python
Fastapi进阶用法,路径参数,路由分发,查询参数等详解
Fastapi进阶用法,路径参数,路由分发,查询参数等详解
|
4天前
|
IDE 测试技术 开发工具
FastAPI 并发请求解析:提高性能的重要特性
在当今的数字化世界中,网络用户对于高速响应和持续连接的诉求日益显著。这促使了基于 Python 构建的 FastAPI 框架受到广泛关注,它不仅现代化且效率极高,而且简化了并行请求的处理。本篇文章旨在探讨 FastAPI 如何处理这类请求,并对应用实例进行实际编码展示。
FastAPI(54)- 详解 Request 请求对象(上)
FastAPI(54)- 详解 Request 请求对象(上)
538 0
|
12月前
|
网络安全 Windows
基于fastapi实现6个接口(token拦截, 2个业务流程,接口参数依赖校验)已经通过postman测试,记录部署服务器和windows,用于pytest接口自动化框架的接口测试对象
基于fastapi实现6个接口(token拦截, 2个业务流程,接口参数依赖校验)已经通过postman测试,记录部署服务器和windows,用于pytest接口自动化框架的接口测试对象
|
存储 SQL 前端开发
FastAPI第三天---文件请求
FastAPI第三天---文件请求
165 0
FastAPI第三天---文件请求
|
前端开发 中间件 API
FastAPI第二天---参数校验
FastAPI第二天---参数校验
184 0
FastAPI第二天---参数校验
|
前端开发 中间件 测试技术
FastApi的请求拦截
FastApi的请求拦截
688 0