跳转至

请求

Request declaration⚓︎

First, you need to import BaseModel from pydantic:

1
from pydantic import BaseModel

path⚓︎

Request parameter in rules,@app.get('/book/{id}').

You have to declare path model as a class that inherits from BaseModel:

1
2
3
4
5
6
7
class BookPath(BaseModel):
    id: int = Field(..., description="book id")


@app.get("/book/{id}", tags=[book_tag], security=security)
async def get_book(path: BookPath):
    ...

query⚓︎

Receive request query parameters.

like path, you need pass query to view function.

1
2
3
4
5
6
7
8
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/{id}', tags=[book_tag], security=security)
async def get_book(path: BookPath, query: BookQuery):
    ...

form⚓︎

Receive request form data and files.

1
2
3
4
5
6
7
8
class UploadFileForm(BaseModel):
    file: UploadFile  # request.files["file"]
    file_type: str = Field(None, description="File type")


@app.post("/upload")
async def upload_file(form: UploadFileForm):
    ...

body⚓︎

Receive request body.

1
2
3
4
5
6
7
8
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])
async def create_book(body: BookBody):
    ...

Receive request headers.

Receive request cookies.

request⚓︎

Receive request from starlette.requests.Request.

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.

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
49
50
from pydantic import BaseModel
from starlette.requests import Request
from starlette.responses import JSONResponse
from starlette.testclient import TestClient

from star_openapi import OpenAPI, RequestBody
from star_openapi.utils import get_model_schema

app = OpenAPI()

client = TestClient(app)


class JsonModel(BaseModel):
    name: str
    age: int


request_body_json = RequestBody(
    description="The json request body",
    content={"application/custom+json": {"schema": get_model_schema(JsonModel)}},
)


@app.post("/json", request_body=request_body_json)
async def get_json(request: Request, body: JsonModel):
    print(request.headers.get("content-type"))
    print(body.model_json_schema())
    return JSONResponse({"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)
async def get_csv(request: Request):
    print(request.headers.get("content-type"))
    return JSONResponse({"message": "Hello World"})


if __name__ == "__main__":
    print(app.routes)
    uvicorn.run(app)