I'm trying to test uploading a file to FastApi, but I keep getting 422 Validation Error. The file upload works in OpenApi interface, but not with the test below.
The router:
@router.post("/files")
def file_contents(files: List[UploadFile]):
return someprocessing(files)
The test (using TestClient
from FastApi):
response = client.post(
url="/files",
files={"files": ("file.xlsx", open("./test_files/file.xlsx", "rb"))},
headers={**auth_headers, **{"Content-Type": "multipart/form-data"}},
)
The error:
{"detail":[{"loc":["body"],"msg":"value is not a valid dict","type":"type_error.dict"}]}
Update: All good I was sending files to a different url...
CodePudding user response:
I guess the problem here is with passed content type, this example works:
from typing import List
from fastapi import FastAPI, UploadFile
app = FastAPI()
@app.post("/files")
def file_contents(files: List[UploadFile]):
return {"filenames": [file.filename for file in files]}
tests:
from fastapi.testclient import TestClient
from fast_example import app
client = TestClient(app)
files = [('files', open('so.py', 'rb')), ('files', open('main.py', 'rb'))]
response = client.post(
url="/files",
files=files
)
print(response.json())
# {'filenames': ['so.py', 'main.py']}