I am using the example from the official documentation: https://fastapi.tiangolo.com/tutorial/request-files/#import-file
Server code:
@app.post("/uploadfile")
async def create_upload_file(data: UploadFile = File(...)):
print("> uploaded file:",data.filename)
return {"filename": data.filename}
Client code:
files = {'upload_file': open('config.txt', 'rb')}
resp = requests.post(
url = URL,
files = files)
print(resp.json())
The problem is that the server always responds with error 422:
{'detail': [{'loc': ['body', 'data'], 'msg': 'field required', 'type': 'value_error.missing'}]}
I am using Python 3 on both server and client and the python-multipart package is already installed.
Can someone please tell me what I am doing wrong, what am I missing, how should I fix the code?
Any hints are much appreciated.