๐จ ๐จ & ๐¶
๐ ๐ช ๐ฌ ๐ & ๐จ ๐ ๐ ๐ฐ โ๏ธ 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
๐ฏโโ๏ธ ๐โ ๐ ๐ช ๐จ ๐ฝ & ๐ ๐ ๐จ.