Request Forms and Files¶
Puedes definir archivos y campos de formulario al mismo tiempo usando File y Form.
Información
Para recibir archivos subidos y/o form data, primero instala python-multipart.
Asegúrate de crear un entorno virtual, actívalo y luego instálalo, por ejemplo:
$ pip install python-multipart
Importar File y Form¶
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,
}
Definir parámetros File y Form¶
Crea parámetros de archivo y formulario de la misma manera que lo harías para Body o Query:
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,
}
Los archivos y campos de formulario se subirán como form data y recibirás los archivos y campos de formulario.
Y puedes declarar algunos de los archivos como bytes y algunos como UploadFile.
Advertencia
Puedes declarar múltiples parámetros File y Form en una path operation, pero no puedes también declarar campos Body que esperas recibir como JSON, ya que el request tendrá el body codificado usando multipart/form-data en lugar de application/json.
Esto no es una limitación de FastAPI, es parte del protocolo HTTP.
Resumen¶
Usa File y Form juntos cuando necesites recibir datos y archivos en el mismo request.