Please, this min code give me this error with request: 400 {"detail":"There was an error parsing the body"} but it good working on swagger ui.
main.py
import shutil
import uvicorn
from fastapi import FastAPI, File, UploadFile
app = FastAPI()
@app.post("/upload")
async def upload(file: UploadFile = File(...)):
try:
with open('save.txt', "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
except:
return {'State':'Fail'}
finally:
file.file.close()
return {'State':'Done'}
if __name__ == "__main__":
uvicorn.run("main:app", host="127.0.0.1",
port=8000, log_level="debug", reload="true")
query.py
import requests
url = 'http://localhost:8000/upload'
headers = {'Content-Type':'multipart/form-data'}
files = [
("file", ("file1.txt", open("F://test.txt", "rb"), "text/txt"))
]
response = requests.post(url, files=files, headers=headers)
print(response.status_code, response.text)
# The response is :
# 400 {"detail":"There was an error parsing the body"}