Zum Inhalt

Formulardaten und Dateien im Request

🌐 Translation by AI and humans

This translation was made by AI guided by humans. 🤝

It could have mistakes of misunderstanding the original meaning, or looking unnatural, etc. 🤖

You can improve this translation by helping us guide the AI LLM better.

English version

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

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

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.