I have an application that stores files and additional information e.g. author, comments DB. I can pass an file to FastAPI, but getting issue by passing it together with parameters.
I checked following question How to add multiple body params with fileupload in FastAPI? and this issue [QUESTION] Use UploadFile in Pydantic model #657 but without success.
I tried 2 definitions in the FastAPI endpoint
Option 1
class Properties(BaseModel):
language: Optional[str] = None
author: Optional[str] = None
@app.post("/uploadfile/")
async def uploadfile(params: Properties, file: UploadFile = File(...)):
#internal logic
Option 2
@app.post("/uploadfile/")
async def uploadfile(file: UploadFile = File(...),
language: str = Form(...),
author: Optional[str] = Form(None)):
#internal logic
Client code: Following code is used on client side, but response is 422 Unprocessable Entity for both options.
with open(path, 'rb') as f:
response = requests.post('http://localhost:8005/uploadfile/', data={'language':'en',
'author':'me'}, files={'file': f})
Both options can't be tested from swagger, there I am getting response: value is not a valid dict. Data looks good for me, but maybe I am missing something.
It seems that the client code is wrong, but also there I tried several changes without success.
In advance, thanks for your support!
Solution that works for me
As 'Drdilyor' wrote in his comment I have use option 2, as we are sending file. My issue was within order of arguments. After changing them everything starts working.