Request
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 : int | None = 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 : int | None = 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 .
@validate_request
Sometimes you want to delay the verification request parameters, such as after login verification:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 from flask_openapi import validate_request
def login_required ():
def decorator ( func ):
@wraps ( func )
def wrapper ( * args , ** kwargs ):
if not request . headers . get ( "Authorization" ):
return { "error" : "Unauthorized" }, 401
return func ( * args , ** kwargs )
return wrapper
return decorator
@app . get ( "/book" )
@login_required
@validate_request ()
def get_book ( query : BookQuery ):
...
Custom kwargs are maintained
When your 'auth decorator' injects custom kwargs, these will be passed on to the final function for you to use.
Any kwargs which are part of the 'path' will have been consumed at this point and can only be referenced using the
path.
So avoid using kwarg-names which overlap with the path.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 from flask_openapi import validate_request
from functools import wraps
def login_required ():
def decorator ( func ):
@wraps ( func )
def wrapper ( * args , ** kwargs ):
if not request . headers . get ( "Authorization" ):
return { "error" : "Unauthorized" }, 401
kwargs [ "client_id" ] = "client1234565"
return func ( * args , ** kwargs )
return wrapper
return decorator
@app . get ( "/book" )
@login_required ()
@validate_request ()
def get_book ( query : BookQuery , client_id : str = None ):
print ( f "Current user identified as { client_id } " )
...
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 .
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' , 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 .
RequestBody
Sometimes, you may need to customize the Content-Type in the request body.
Warning
request_body may conflict with the body and form keyword, so try not to use them together unless the
content type wants to be the same.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 from flask import request
from pydantic import BaseModel
from flask_openapi import OpenAPI , RequestBody
from flask_openapi.utils import get_model_schema
app = OpenAPI ( __name__ )
class BookModel ( BaseModel ):
name : str
age : int
request_body_json = RequestBody (
description = "The json request body" ,
content = { "application/custom+json" : { "schema" : get_model_schema ( BookModel )}},
)
@app . post ( "/json" , request_body = request_body_json )
def post_json ( body : BookModel ):
print ( request . headers . get ( "content-type" ))
print ( body . model_json_schema ())
return { "message" : "Hello World" }
request_body = RequestBody (
description = "The multi request body" ,
content = {
"text/plain" : { "schema" : { "type" : "string" }},
"text/html" : { "schema" : { "type" : "string" }},
"image/png" : { "schema" : { "type" : "string" , "format" : "binary" }},
},
)
@app . post ( "/text" , request_body = request_body )
def post_csv ():
print ( request . headers . get ( "content-type" ))
data = request . data
print ( data )
return { "message" : "Hello World" }
if __name__ == "__main__" :
print ( app . url_map )
app . run ()