Skip to content

Request

Request declaration⚓︎

First, you need to import BaseModel from pydantic:

1
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:

1
2
3
4
5
6
7
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

1
from flask import request

like path, you need pass query to view function.

1
2
3
4
5
6
7
8
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):
    ...

form⚓︎

Receive flask request.form and request.files.

1
2
3
4
5
6
7
8
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.

1
2
3
4
5
6
7
8
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.

Receive flask request.cookies.

raw⚓︎

Receive flask request and no data validation.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from flask_openapi3 import RawModel


class BookRaw(RawModel):
    mimetypes = ["text/csv", "application/json"]


@app.post("/book")
def get_book(raw: BookRaw):
    # raw equals to flask.request
    print(raw.data)
    print(raw.mimetype)
    return "ok"

Request model⚓︎

First, you need to define a pydantic model:

1
2
3
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.

However, you can also use Field to extend Parameter Object. Here is an example:

age with example and author with deprecated.

1
2
3
class BookQuery(BaseModel):
    age: int = Field(..., ge=2, le=4, description='Age', json_schema_extra={"example": 3})
    author: str = Field(None, description='Author', json_schema_extra={"deprecated": True})

Magic:

More available fields to see Parameter Object Fixed Fields.