请求
Request declaration
First, you need to import BaseModel
from pydantic
:
| from pydantic import BaseModel
|
path
Request parameter in rules,@app.get('/book/<int:bid>')
.
You have to declare path model as a class that inherits from BaseModel
:
| class BookPath(BaseModel):
bid: int = Field(..., description='book id')
@app.get('/book/<int:bid>', tags=[book_tag], security=security)
def get_book(path: BookPath):
...
|
query
Receive flask request.args
.
Info
| from flask import request
|
like path, you need pass query
to view function.
| class BookQuery(BaseModel):
age: Optional[int] = Field(..., ge=2, le=4, description='Age')
author: str = Field(None, min_length=2, max_length=4, description='Author')
@app.get('/book/<int:bid>', tags=[book_tag], security=security)
def get_book(path: BookPath, query: BookQuery):
...
|
Receive flask request.form
and request.files
.
| class UploadFileForm(BaseModel):
file: FileStorage # request.files["file"]
file_type: str = Field(None, description="File type")
@app.post('/upload')
def upload_file(form: UploadFileForm):
...
|
body
Receive flask request.json
.
| class BookBody(BaseModel):
age: Optional[int] = Field(..., ge=2, le=4, description='Age')
author: str = Field(None, min_length=2, max_length=4, description='Author')
@app.post('/book', tags=[book_tag])
def create_book(body: BookBody):
...
|
Receive flask request.headers
.
cookie
Receive flask request.cookies
.
Request model
First, you need to define a pydantic model:
| class BookQuery(BaseModel):
age: int = Field(..., ge=2, le=4, description='Age')
author: str = Field(None, description='Author')
|
More information to see BaseModel, and you can Customize the Field.
New in v2.1.0
However, you can also use Field to extend Parameter Object. Here is an example:
age
with example
and author
with deprecated
.
| class BookQuery(BaseModel):
age: int = Field(..., ge=2, le=4, description='Age', example=3)
author: str = Field(None, description='Author', deprecated=True)
|
Magic:
![](../../../assets/Snipaste_2022-09-04_10-10-03.png)
More available fields to see Parameter Object Fixed Fields.