post请求一般的请求体都是json格式的
在这里我们需要引入一个新的包,下面这个包就是用来定义入参的消息体的
pip3 install pydantic
请求地址
http://127.0.0.1:8100/fastapi/
请求体(均使用默认值,所以请求体为空)
{
}
代码
"""
@File : fastapiOne.py
@Desciption:
"""
import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class People(BaseModel): # 继承了BaseModel,定义了People的数据格式
name: str = None # 默认了name的值为None
age: int = 18 # 默认了age为18
sex: str = "renyao" # 默认了sex为renyao
@app.post("/fastapi/")
async def postdate(people: People): # 传入一个People类型的参数people
return people
if __name__ == "__main__":
uvicorn.run(app='fastapiOne:app', host='127.0.0.1', port=8100, reload=True, debug=True)
结果
{
"name": null,
"age": 18,
"sex": "renyao"
}
请求体(均使用自定义的值)
{
"name":"zhangsan",
"age":18,
"sex":"nan"
}
结果
{
"name": "zhangsan",
"age": 18,
"sex": "nan"
}
请求地址
http://127.0.0.1:8100/fastapi/777/?a=666&b=fawai
请求体
{
"name":"zhangsan",
"age":18,
"sex":"nan"
}
代码
"""
@File : fastapiOne.py
@Desciption:
"""
import uvicorn
from fastapi import FastAPI, Query
from pydantic import BaseModel
app = FastAPI()
class People(BaseModel): # 继承了BaseModel,定义了People的数据格式
name: str = None # 默认了name的值为None
age: int = 18 # 默认了age为18
sex: str = "renyao" # 默认了sex为renyao
@app.post("/fastapi/{huaju}")
async def postdate(*,
huaju: int=(9), # 获取path参数中的huaju,int类型,默认值为9
a: int = Query(999), # 设置Query参数a,int类型,默认值为999
b: str = Query("lisi"), # 设置Query参数b,str类型,默认值为lisi
people: People # 传入一个People类型的参数people
):
return {"huaju": huaju, "a": a, "b": b, "people": people}
if __name__ == "__main__":
uvicorn.run(app='fastapiOne:app', host='127.0.0.1', port=8100, reload=True, debug=True)
结果
{
"huaju": 777,
"a": 666,
"b": "fawai",
"people": {
"name": "zhangsan",
"age": 18,
"sex": "nan"
}
}
相对于上面的例子,我们把Query参数去掉了
请求地址
http://127.0.0.1:8100/fastapi/777
请求体
{
"name":"zhangsan",
"age":18,
"sex":"nan"
}
代码
"""
@File : fastapiOne.py
@Desciption:
"""
import uvicorn
from fastapi import FastAPI, Query
from pydantic import BaseModel
app = FastAPI()
class People(BaseModel): # 继承了BaseModel,定义了People的数据格式
name: str = None # 默认了name的值为None
age: int = 18 # 默认了age为18
sex: str = "renyao" # 默认了sex为renyao
@app.post("/fastapi/{huaju}")
async def postdate(*,
people: People # 传入一个People类型的参数people
):
return {"people": people}
if __name__ == "__main__":
uvicorn.run(app='fastapiOne:app', host='127.0.0.1', port=8100, reload=True, debug=True)
结果
{
"people": {
"name": "zhangsan",
"age": 18,
"sex": "nan"
}
}
继续上面的例子,代码不变
请求地址
http://127.0.0.1:8100/fastapi/777/?a=666&b=fawai
请求体
{
"name":"zhangsan",
"age":18,
"sex":"nan"
}
结果
{
"people": {
"name": "zhangsan",
"age": 18,
"sex": "nan"
}
}
发现没什么变化哇,那岂不是很不严谨?真正不一样的地方在日志的打印上
日志
INFO: 127.0.0.1:53162 - "POST /fastapi/777/?a=666&b=fawai HTTP/1.1" 307 Temporary Redirect
INFO: 127.0.0.1:53162 - "POST /fastapi/777?a=666&b=fawai HTTP/1.1" 200 OK
我们发现先执行了我们的初始请求/fastapi/777/?a=666&b=fawai,状态为307,我们都知道3开头的状态都是重定向,说明服务器也发现了一场,但是马上紧接着就发起了一条新的请求/fastapi/777?a=666&b=fawai,请求成功了,这才是我们接口返回的真正请求地址,只不过是在前端看来被屏蔽了没看到而已
其实这个也没啥写的,就是少了一层path而已
请求地址
http://127.0.0.1:8100/fastapi/?a=666&b=fawai
请求体
{
"name":"zhangsan",
"age":18,
"sex":"nan"
}
代码
"""
@File : fastapiOne.py
@Desciption:
"""
import uvicorn
from fastapi import FastAPI, Query
from pydantic import BaseModel
app = FastAPI()
class People(BaseModel): # 继承了BaseModel,定义了People的数据格式
name: str = None # 默认了name的值为None
age: int = 18 # 默认了age为18
sex: str = "renyao" # 默认了sex为renyao
@app.post("/fastapi/")
async def postdate(*,
a: int = Query(999), # 设置Query参数a,int类型,默认值为999
b: str = Query("lisi"), # 设置Query参数b,str类型,默认值为lisi
people: People # 传入一个People类型的参数people
):
return {"a": a, "b": b, "people": people}
if __name__ == "__main__":
uvicorn.run(app='fastapiOne:app', host='127.0.0.1', port=8100, reload=True, debug=True)
结果
{
"a": 666,
"b": "fawai",
"people": {
"name": "zhangsan",
"age": 18,
"sex": "nan"
}
}
请求地址
http://127.0.0.1:8100/fastapi/
请求体
{
"people":{
"name":"zhangsan",
"age":18,
"sex":"nan"
},
"animal":{
"name":"danchaofan",
"wuzhong":"miaomiao"
}
}
代码
"""
@File : fastapiOne.py
@Desciption:
"""
import uvicorn
from fastapi import FastAPI, Query
from pydantic import BaseModel
app = FastAPI()
class People(BaseModel): # 继承了BaseModel,定义了People的数据格式
name: str = None # 默认了name的值为None
age: int = 18 # 默认了age为18
sex: str = "renyao" # 默认了sex为renyao
class Animal(BaseModel):
name: str
wuzhong: str = "mao"
@app.post("/fastapi/")
async def postdate(
people: People, # 传入一个People类型的参数people
animal: Animal
):
return {"people": people, "animal": animal}
if __name__ == "__main__":
uvicorn.run(app='fastapiOne:app', host='127.0.0.1', port=8100, reload=True, debug=True)
结果
{
"people": {
"name": "zhangsan",
"age": 18,
"sex": "nan"
},
"animal": {
"name": "danchaofan",
"wuzhong": "miaomiao"
}
}
请求地址
http://127.0.0.1:8100/fastapi/
请求体
{
"name":"danchaofan",
"wuzhong":"miaomiao",
"people":{
"name":"zhangsan",
"age":18,
"sex":"nan"
}
}
代码
"""
@File : fastapiOne.py
@Desciption:
"""
import uvicorn
from fastapi import FastAPI, Query
from pydantic import BaseModel
app = FastAPI()
class People(BaseModel): # 继承了BaseModel,定义了People的数据格式
name: str = None # 默认了name的值为None
age: int = 18 # 默认了age为18
sex: str = "renyao" # 默认了sex为renyao
class Animal(BaseModel):
name: str
wuzhong: str = "mao"
people: People = None
@app.post("/fastapi/")
async def postdate(
animal: Animal
):
return {"animal": animal}
if __name__ == "__main__":
uvicorn.run(app='fastapiOne:app', host='127.0.0.1', port=8100, reload=True, debug=True)
结果
{
"animal": {
"name": "danchaofan",
"wuzhong": "miaomiao",
"people": {
"name": "zhangsan",
"age": 18,
"sex": "nan"
}
}
}
这样有两种方法
将最外层定义为一个对象,个人认为是比较方便的
请求地址
http://127.0.0.1:8100/fastapi/
请求体
{
"one":22,
"people":{
"name":"zhangsan",
"age":18,
"sex":"nan"
},
"animal":{
"name":"danchaofan",
"wuzhong":"miaomiao"
}
}
代码
"""
@File : fastapiOne.py
@Desciption:
"""
import uvicorn
from fastapi import FastAPI, Query
from pydantic import BaseModel
app = FastAPI()
class People(BaseModel): # 继承了BaseModel,定义了People的数据格式
name: str = None # 默认了name的值为None
age: int = 18 # 默认了age为18
sex: str = "renyao" # 默认了sex为renyao
class Animal(BaseModel):
name: str
wuzhong: str = "mao"
class All(BaseModel):
one: int
people: People
animal: Animal
@app.post("/fastapi/")
async def postdate(
all: All
):
return {"people": all.people, "animal": all.animal} # 再将people和animal从all中提取出来
if __name__ == "__main__":
uvicorn.run(app='fastapiOne:app', host='127.0.0.1', port=8100, reload=True, debug=True)
结果
{
"people": {
"name": "zhangsan",
"age": 18,
"sex": "nan"
},
"animal": {
"name": "danchaofan",
"wuzhong": "miaomiao"
}
}
请求地址
http://127.0.0.1:8100/fastapi/9/?six=6
请求体
{
"one":22,
"people":{
"name":"zhangsan",
"age":18,
"sex":"nan"
},
"animal":{
"name":"danchaofan",
"wuzhong":"miaomiao"
}
}
代码
"""
@File : fastapiOne.py
@Desciption:
"""
import uvicorn
from fastapi import FastAPI, Body
from pydantic import BaseModel
app = FastAPI()
class People(BaseModel): # 继承了BaseModel,定义了People的数据格式
name: str = None # 默认了name的值为None
age: int = 18 # 默认了age为18
sex: str = "renyao" # 默认了sex为renyao
class Animal(BaseModel):
name: str
wuzhong: str = "mao"
@app.post("/fastapi/{new_id}")
async def postdate(*,
new_id: int,
six: int,
people: People,
animal: Animal,
one: int = Body(..., gt=0)
):
return {"new_id": new_id, "six": six, "people": people, "animal": animal, "one": one}
if __name__ == "__main__":
uvicorn.run(app='fastapiOne:app', host='127.0.0.1', port=8100, reload=True, debug=True)
结果
{
"new_id": 9,
"six": 6,
"people": {
"name": "zhangsan",
"age": 18,
"sex": "nan"
},
"animal": {
"name": "danchaofan",
"wuzhong": "miaomiao"
},
"one": 22
}
也是有两种办法
好记,这就是我为什么喜欢这种模式,就是对象套对象嘛
请求地址
http://127.0.0.1:8100/fastapi/9?six=6
请求体
{
"people":{
"name":"zhangsan",
"age":18,
"sex":"nan"
}
}
代码
"""
@File : fastapiOne.py
@Desciption:
"""
import uvicorn
from fastapi import FastAPI, Body, Query, Path
from pydantic import BaseModel
app = FastAPI()
class People(BaseModel): # 继承了BaseModel,定义了People的数据格式
name: str = None # 默认了name的值为None
age: int = 18 # 默认了age为18
sex: str = "renyao" # 默认了sex为renyao
class Animal(BaseModel):
people: People
@app.post("/fastapi/{new_id}")
async def postdate(*,
new_id: int = Path(gt=0, lt=10, default=..., description="newid"),
six: int = Query(default=9, gt=1, lt=11, description="six"),
animal: Animal,
):
return {"new_id": new_id, "six": six, "people": animal.people} # 还需要将people从animal中取出来
if __name__ == "__main__":
uvicorn.run(app='fastapiOne:app', host='127.0.0.1', port=8100, reload=True, debug=True)
结果
{
"new_id": 9,
"six": 6,
"people": {
"name": "zhangsan",
"age": 18,
"sex": "nan"
}
}
请求地址和请求体不变
代码
"""
@File : fastapiOne.py
@Desciption:
"""
import uvicorn
from fastapi import FastAPI, Body, Query, Path
from pydantic import BaseModel
app = FastAPI()
class People(BaseModel): # 继承了BaseModel,定义了People的数据格式
name: str = None # 默认了name的值为None
age: int = 18 # 默认了age为18
sex: str = "renyao" # 默认了sex为renyao
@app.post("/fastapi/{new_id}")
async def postdate(*,
new_id: int = Path(gt=0, lt=10, default=..., description="newid"),
six: int = Query(default=9, gt=1, lt=11, description="six"),
people: People = Body(default=..., embed=True),
):
return {"new_id": new_id, "six": six, "people": people}
if __name__ == "__main__":
uvicorn.run(app='fastapiOne:app', host='127.0.0.1', port=8100, reload=True, debug=True)
结果
{
"new_id": 9,
"six": 6,
"people": {
"name": "zhangsan",
"age": 18,
"sex": "nan"
}
}
这三个参数吧,其实有很多功能,比如限制大小等,我们来看看基础的功能
"""
@File : fastapiOne.py
@Desciption:
"""
import uvicorn
from fastapi import FastAPI, Body, Query, Path
from pydantic import BaseModel
app = FastAPI()
class People(BaseModel): # 继承了BaseModel,定义了People的数据格式
name: str = None # 默认了name的值为None
age: int = 18 # 默认了age为18
sex: str = "renyao" # 默认了sex为renyao
class Animal(BaseModel):
name: str
wuzhong: str = "mao"
@app.post("/fastapi/{new_id}")
async def postdate(*,
new_id: int = Path(gt=0, lt=10, default=..., description="newid"),
six: int = Query(default=9, gt=1, lt=11, description="six"),
people: People,
animal: Animal,
one: int = Body(default=..., description="one", gt=0)
):
return {"new_id": new_id, "six": six, "people": people, "animal": animal, "one": one}
if __name__ == "__main__":
uvicorn.run(app='fastapiOne:app', host='127.0.0.1', port=8100, reload=True, debug=True)
gt大于
lt小于
default默认值
description描述
可能这些东西写在代码里比较抽象,比如description有什么用呢,我们看不到摸不着的,不,我们可以看到,还记得下面这个地址么
http://127.0.0.1:8100/docs