3.FastAPI参数
Posted Janeh1018
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了3.FastAPI参数相关的知识,希望对你有一定的参考价值。
3.FastAPI参数
3.1路径参数(Path参数)
路径参数就是在请求路径中携带的参数。
3.1.1路径参数的声明
在请求路径中使用花括号标识参数,在对应的Python函数中使用函数参数接受参数值。
示例:
from fastapi import FastAPI
app = FastAPI()
@app.get(path=/language/language_id)
async def find_language(language_id):
languages =
1: Python,
2: C,
3: C++,
4: Java,
5: C#,
6: Go,
7: Rust,
8: Julia,
9: R
return lang: languages[int(language_id)]
上面的示例中,路径中的language_id声明了一个路径参数language_id,对应的函数find_language(language_id)中通过定义同名的函数参数来接受数据值,但此处未对参数类型进行定义,所以在返回数据时采用int(language_id)对数据值进行了类型转换。
执行请求:
curl http://127.0.0.1:8000/language/9
"lang":"R"
3.1.2路径参数类型
路径参数的类型使用标准的Python类型在函数中声明,代码如下:
from fastapi import FastAPI
app = FastAPI()
@app.get(path=/language/language_id)
async def find_language(language_id: int):
languages =
1: Python,
2: C,
3: C++,
4: Java,
5: C#,
6: Go,
7: Rust,
8: Julia,
9: R
return lang: languages[language_id]
在上面的代码中函数参数language_id被声明为int类型。
设置参数类型的好处:
- 可以提供编辑器支持,如:错误检查、完整性检查等
- 可以自动转换路径参数类型,如在上面的请求中,如果language_id不声明int类型,则按照字符串进行处理,声明为int类型后,则可自动转换为int类型。
- 另外,还可以提供数据类型的校验,如当执行如下请求时会返回:
curl http://127.0.0.1:8000/language/python
"detail":[
"loc":["path","language_id"],
"msg":"value is not a valid integer",
"type":"type_error.integer"
]
从以上返回的信息中可以看到:路径参数language_id出现了整型类型错误。
3.1.3路径参数顺序
在设计系统URL路径时,有时会出现一些固定的路径与路径参数冲突的情况,比如:/language/10是一个固定路径,我们设计该路径返回‘lang’: Fortran这样一个固定的数据,这样就引起了路径冲突。
由于路径操作时按照顺序来评估的,所以要确保/language/10在/language/language_id之前进行声明。代码如下:
- /language/10在/language/language_id之后声明
from fastapi import FastAPI
app = FastAPI()
@app.get(path=/language/language_id)
async def find_language(language_id: int):
languages =
1: Python,
2: C,
3: C++,
4: Java,
5: C#,
6: Go,
7: Rust,
8: Julia,
9: R
return lang: languages[language_id]
@app.get(path=/language/10)
async def find_language():
return lang: Fortran
执行请求:
curl http://127.0.0.1:8000/language/10
Internal Server Error
可见,没有匹配到正确的url上,所以出现错误。
- /language/10在/language/language_id之前声明
from fastapi import FastAPI
app = FastAPI()
@app.get(path=/language/10)
async def find_language():
return lang: Fortran
@app.get(path=/language/language_id)
async def find_language(language_id: int):
languages =
1: Python,
2: C,
3: C++,
4: Java,
5: C#,
6: Go,
7: Rust,
8: Julia,
9: R
return lang: languages[language_id]
执行请求:
curl http://127.0.0.1:8000/language/10
"lang":"Fortran"
3.2查询参数(Query参数)
查询参数是在URL中"?"之后的键值对,每对键值对用"&"分开。在函数声明时,当声明不属于路径参数的其他参数时,FastAPI会将其解释为查询参数。
3.2.1声明查询参数
from fastapi import FastAPI
app = FastAPI()
@app.get(path=/add)
async def find_language(vara: int = 0, varb: int = 0):
return added: vara + varb
执行请求:
curl http://127.0.0.1:8000/add?"vara=2&varb=3"
"added":5
在上面的代码中,函数声明的参数vara和varb由于在路径中均没有声明,所以FastAPI会解析成查询参数;同时在代码中还为参数定义了默认值,那么当请求http://127.0.0.1:8000/add时,执行结果:
curl http://127.0.0.1:8000/add
"added":0
curl http://127.0.0.1:8000/add?varb=9
"added":9
curl http://127.0.0.1:8000/add?vara=8
"added":8
3.2.2布尔类型的查询参数
当查询参数被声明为bool类型时,传入的参数会被转换,代码如下:
from fastapi import FastAPI
app = FastAPI()
@app.get(path=/boolean)
async def find_language(ok: bool = False):
if ok:
return msg: Your answer is yes
else:
return msg: Your answer is no
执行请求:
curl http://127.0.0.1:8000/boolean?ok=1
"msg":"Your answer is yes"
curl http://127.0.0.1:8000/boolean?ok=0
"msg":"Your answer is no"
curl http://127.0.0.1:8000/boolean?ok=True
"msg":"Your answer is yes"
curl http://127.0.0.1:8000/boolean?ok=true
"msg":"Your answer is yes"
curl http://127.0.0.1:8000/boolean?ok=False
"msg":"Your answer is no"
curl http://127.0.0.1:8000/boolean?ok=false
"msg":"Your answer is no"
curl http://127.0.0.1:8000/boolean?ok=Yes
"msg":"Your answer is yes"
curl http://127.0.0.1:8000/boolean?ok=yes
"msg":"Your answer is yes"
curl http://127.0.0.1:8000/boolean?ok=No
"msg":"Your answer is no"
curl http://127.0.0.1:8000/boolean?ok=no
"msg":"Your answer is no"
curl http://127.0.0.1:8000/boolean?ok=on
"msg":"Your answer is yes"
curl http://127.0.0.1:8000/boolean?ok=On
"msg":"Your answer is yes"
curl http://127.0.0.1:8000/boolean?ok=Off
"msg":"Your answer is no"
curl http://127.0.0.1:8000/boolean?ok=off
"msg":"Your answer is no"
3.2.3同时使用路径参数和查询参数
可以同时声明多个路径参数和查询参数,FastAPI按参数名区分参数,与声明顺序无关。
示例代码:
from fastapi import FastAPI
app = FastAPI()
@app.get(path=/add/vara/varb)
async def add(vara: int, varb: int, x: int, y: int):
return added: vara + varb, multiply: x * y
@app.get(path=/multiply/vara/varb)
async def multiply(x: int, y: int, vara: int, varb: int):
return added: vara + varb, multiply: x * y
执行请求:
curl http://127.0.0.1:8000/add/2/3?"x=5&y=6"
"added":5,"multiply":30
curl http://127.0.0.1:8000/multiply/2/3?"x=5&y=6"
"added":5,"multiply":30
C:\\Users\\Administrator>
3.2.4必须的查询参数
声明查询参数时,如果参数未定义默认值,则该参数就是必须的查询参数。代码如下:
from fastapi import FastAPI
app = FastAPI()
@app.get(path=/add/vara/varb)
async def add(vara: int, varb: int, x: int, y: int = 1):
return added: vara + varb, multiply: x * y
以上代码中,参数x未声明默认值,当执行如下请求时,会出现错误提示:
curl http://127.0.0.1:8000/add/2/3
"detail":[
"loc":["query","x"],
"msg":"field required",
"type":"value_error.missing"
]
错误提示x为必须的查询参数。
以上是关于3.FastAPI参数的主要内容,如果未能解决你的问题,请参考以下文章
xml Eclipse模板(代码片段)检查参数并最终抛出IllegalArgumentException