Skip to content

๐Ÿ“จ ๐Ÿ“จ & ๐Ÿ“

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”ฌ ๐Ÿ“ & ๐Ÿ“จ ๐Ÿ‘ ๐ŸŽ ๐Ÿ•ฐ โš™๏ธ File & Form.

Info

๐Ÿ“จ ๐Ÿ“‚ ๐Ÿ“ & /โš–๏ธ ๐Ÿ“จ ๐Ÿ“Š, ๐Ÿฅ‡ โŽ python-multipart.

๐Ÿคถ โ“‚. pip install python-multipart.

๐Ÿ—„ File & Form

from fastapi import FastAPI, File, Form, UploadFile

app = FastAPI()


@app.post("/files/")
async def create_file(
    file: bytes = File(), fileb: UploadFile = File(), token: str = Form()
):
    return {
        "file_size": len(file),
        "token": token,
        "fileb_content_type": fileb.content_type,
    }

๐Ÿ”ฌ File & Form ๐Ÿ”ข

โœ ๐Ÿ“ & ๐Ÿ“จ ๐Ÿ”ข ๐ŸŽ ๐ŸŒŒ ๐Ÿ‘† ๐Ÿ”œ Body โš–๏ธ Query:

from fastapi import FastAPI, File, Form, UploadFile

app = FastAPI()


@app.post("/files/")
async def create_file(
    file: bytes = File(), fileb: UploadFile = File(), token: str = Form()
):
    return {
        "file_size": len(file),
        "token": token,
        "fileb_content_type": fileb.content_type,
    }

๐Ÿ“ & ๐Ÿ“จ ๐Ÿ‘ ๐Ÿ”œ ๐Ÿ“‚ ๐Ÿ“จ ๐Ÿ“Š & ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ“จ ๐Ÿ“ & ๐Ÿ“จ ๐Ÿ‘.

& ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ“ bytes & UploadFile.

Warning

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ’— File & Form ๐Ÿ”ข โžก ๐Ÿ› ๏ธ, โœ‹๏ธ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿšซ ๐Ÿ“ฃ Body ๐Ÿ‘ ๐Ÿ‘ˆ ๐Ÿ‘† โŒ› ๐Ÿ“จ ๐ŸŽป, ๐Ÿ“จ ๐Ÿ”œ โœ”๏ธ ๐Ÿ’ช ๐Ÿ—œ โš™๏ธ multipart/form-data โ†ฉ๏ธ application/json.

๐Ÿ‘‰ ๐Ÿšซ ๐Ÿšซ FastAPI, โšซ๏ธ ๐Ÿ• ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐Ÿ› ๏ธ.

๐ŸŒƒ

โš™๏ธ File & Form ๐Ÿ‘ฏโ€โ™‚๏ธ ๐Ÿ•โ” ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“จ ๐Ÿ’ฝ & ๐Ÿ“ ๐ŸŽ ๐Ÿ“จ.