This is the 7th day of my participation in the August Text Challenge.More challenges in August
Problems thrown
We’ve seen query parameters before, but in real development we may need to specify the type, length, and other attributes of the parameters. At this point we need to validate the query parameters.
The type can be limited by the display type, but other attributes such as length need to be implemented with FastApi’s Query object.
The instance
implementation
Parameter qualification, we need to use the FastAPI Query object to achieve.
The import of the Query
from fastapi import Query
Copy the code
Specifies the maximum length of a parameter
Use argument: max_length
@app.get('/len')
async def get_len(q:str=Query(. ,max_length=2)) :
res = {'name':'phyger'}
if q:
res.update({'q':q})
return res
Copy the code
From the above tests, we know:
Query parameter q is optional. The maximum length is 2. If the maximum length is exceeded, fastAPI will report an error.
Specifies the minimum length of a parameter
Use argument: min_length
@app.get('/len')
async def get_len(q:Optional[str]=Query(None,min_length=2,max_length=5)) :
res = {'name':'phyger'}
if q:
res.update({'q':q})
return res
Copy the code
Query parameters of the Query type must have default values. Like: the Query (… ,min_length=2,max_length=5)
In addition to limiting the length, we can also passregex
Property to constrain the format of arguments through regular expressions
Default Query parameters
Optional Query parameters: Usually we use Optional with the default value of Query;
async def get_len(q:Optional[str]=Query(None,min_length=2,max_length=5)) :
pass
Copy the code
async def get_len(q:str=Query(None,min_length=2,max_length=5)) :
pass
Copy the code
Either way is fine.
Mandatory Query parameters: Normally we specify a default value for Query… To implement.
async def get_len(q:str=Query(. ,min_length=2,max_length=5)) :
pass
Copy the code
When Optional types and… If both default parameters exist, query parameters are still mandatory.
Multiple query parameters
When we need to design multiple query parameters, we can write this.
from typing import List
from fastapi import Query
@app.get('/len')
async def get_len(q:Optional[List[str]]=Query(. ,min_length=2,max_length=5)) :
res = {'name':'phyger'}
if q:
res.update({'q':q})
return res
Copy the code
You can also use list directly
from typing import List
from fastapi import Query
@app.get('/len')
async def get_len(q:Optional[list]=Query(.)) :
res = {'name':'phyger'}
if q:
res.update({'q':q})
return res
Copy the code
More metadata
title
The title property of Query lets us specify the title of the parameter to use as a hint.
from fastapi import Query
@app.get('/title/')
async def title(name:str = Query(. ,title="test title",max_length=5,min_length=2)) :
return {'result':name}
Copy the code
description
from fastapi import Query
@app.get('/title/')
async def title(name:str = Query(. ,title="test title",max_length=5,min_length=2,description='test desc')) :
return {'result':name}
Copy the code
Effects in DOC
The alias
In practice, query parameters can sometimes look like x-name, which is not a valid variable in Python. FastApi provides aliases to solve this problem.
from fastapi import Query
@app.get('/title/')
async def title(name:str = Query(. ,max_length=5,min_length=2,alias='x-name')) :
return {'result':name}
Copy the code
Parameters of the abandoned
When we plan to deprecate parameters in a later release, we can use the deprecated attribute to alert users.
from fastapi import Query
@app.get('/title/')
async def title(name:str = Query(. ,max_length=5,min_length=2,alias='x-name',deprecated=True)) :
return {'result':name}
Copy the code
Effects in the interface document
Thank you for reading, don’t forget to follow, like, comment, forward four consecutive yo!