Description
I've tried a sample code for upload my file but it failed.
from fastapi import FastAPI, File, UploadFile
app = FastAPI()
@app.post("/files/")
async def create_file(file: bytes = File(...)):
return {"file_size": len(file)}
@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...)):
return {"filename": file.filename}
Result
Which of the two endpoints failed? And what version of FastAPI are you using?
@dmontagu both endpoints failed. How to check version of FastAPI ?
pip list or from fastapi import __version__; print(__version__)
ok..as requested..
fastapi version = 0.33.0

Hmm.. this could be a bug in the swagger docs. I would try manually sending the file using the curl command the swagger docs print out (and maybe also updating to 0.35.0 if possible).
I've try update the FastApi version to 0.35.0 and manually sending the file using curl, but the result still same. Could you assist to fix it ?
FastApi version

Curl command result from server.
@amanjazari If you can share a self-contained script (that runs in uvicorn) and the curl command you are using (in a copyable form, rather than a screenshot), I will make any modifications necessary to get it to work for me locally.
For what it's worth, I am using file upload functionality in several places, so it should definitely be possible to get this to work.
@dmontagu ,
Below is the script i used to test file upload as per tutorial at
https://fastapi.tiangolo.com/tutorial/request-files/
from fastapi import FastAPI, File, UploadFile
app = FastAPI()
@app.post("/files/")
async def create_file(file: bytes = File(...)):
return {"file_size": len(file)}
@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...)):
return {"filename": file.filename}
Curl command at swagger
curl -X POST "http://url/uploadfile/" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "[email protected];type=text/plain"
@amanjazari
It's worth checking if you have python-multipart installed in your environment.
Via pip install python-multipart you will install the package and this may well solve your issue.
EDIT: This tip is also present in the official docs now: https://fastapi.tiangolo.com/tutorial/request-files/
@davidefiocco My problem was identical and installing python-multipart solved it. Thanks!
Thanks for the help and discussion here everyone!
Was your issue solved @amanasmuei ?
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.
@amanjazari
It's worth checking if you have
python-multipartinstalled in your environment.If
pip install python-multipartinstalls the package as you don't have it already, this may solve your issue.
Thanks a lot dude. This fixed the issue for me
Christ, i spent half a day on this. why don't you have python-multipart as a requirement of this module?!?!?
@whillas the indication is prominent in the guide https://fastapi.tiangolo.com/tutorial/request-files (at the very top), but indeed @tiangolo it seems that time gets lost on this (it happened to me too at the time)
The latest versions (for quite a while now) detect if you use something that would require python-multipart and error out quickly giving you instructions about how to fix it.
Also, @whillas, that's not the way to ask. I'm giving you this for free, working on it in my free time. And everyone here is helping in their free time mostly. Please try to be more respectful.
Most helpful comment
@amanjazari
It's worth checking if you have
python-multipartinstalled in your environment.Via
pip install python-multipartyou will install the package and this may well solve your issue.EDIT: This tip is also present in the official docs now: https://fastapi.tiangolo.com/tutorial/request-files/