Formulardaten und Dateien im Request¶
Sie können gleichzeitig Dateien und Formulardaten mit File und Form definieren.
Info
Um hochgeladene Dateien und/oder Formulardaten zu empfangen, installieren Sie zuerst python-multipart.
Stellen Sie sicher, dass Sie eine virtuelle Umgebung erstellen, diese aktivieren und es dann installieren, z. B.:
$ pip install python-multipart
File und Form importieren¶
from typing import Annotated
from fastapi import FastAPI, File, Form, UploadFile
app = FastAPI()
@app.post("/files/")
async def create_file(
file: Annotated[bytes, File()],
fileb: Annotated[UploadFile, File()],
token: Annotated[str, Form()],
):
return {
"file_size": len(file),
"token": token,
"fileb_content_type": fileb.content_type,
}
🤓 Other versions and variants
from fastapi import FastAPI, File, Form, UploadFile
from typing_extensions import Annotated
app = FastAPI()
@app.post("/files/")
async def create_file(
file: Annotated[bytes, File()],
fileb: Annotated[UploadFile, File()],
token: Annotated[str, Form()],
):
return {
"file_size": len(file),
"token": token,
"fileb_content_type": fileb.content_type,
}
Tip
Prefer to use the Annotated version if possible.
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 und Form-Parameter definieren¶
Erstellen Sie Datei- und Formularparameter, so wie Sie es auch mit Body oder Query machen würden:
from typing import Annotated
from fastapi import FastAPI, File, Form, UploadFile
app = FastAPI()
@app.post("/files/")
async def create_file(
file: Annotated[bytes, File()],
fileb: Annotated[UploadFile, File()],
token: Annotated[str, Form()],
):
return {
"file_size": len(file),
"token": token,
"fileb_content_type": fileb.content_type,
}
🤓 Other versions and variants
from fastapi import FastAPI, File, Form, UploadFile
from typing_extensions import Annotated
app = FastAPI()
@app.post("/files/")
async def create_file(
file: Annotated[bytes, File()],
fileb: Annotated[UploadFile, File()],
token: Annotated[str, Form()],
):
return {
"file_size": len(file),
"token": token,
"fileb_content_type": fileb.content_type,
}
Tip
Prefer to use the Annotated version if possible.
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,
}
Die Datei- und Formularfelder werden als Formulardaten hochgeladen, und Sie erhalten diese Dateien und Formularfelder.
Und Sie können einige der Dateien als bytes und einige als UploadFile deklarieren.
Achtung
Sie können mehrere File- und Form-Parameter in einer Pfadoperation deklarieren, aber Sie können nicht auch Body-Felder deklarieren, die Sie als JSON erwarten, da der Body des Request mittels multipart/form-data statt application/json kodiert sein wird.
Das ist keine Limitation von FastAPI, sondern Teil des HTTP-Protokolls.
Zusammenfassung¶
Verwenden Sie File und Form zusammen, wenn Sie Daten und Dateien zusammen im selben Request empfangen müssen.