I'm using FastAPI to write an API to upload text files and to read the txt file contents.
@app.post('/read_txt_file', response_class=PlainTextResponse)
async def upload_file_and_read(file: UploadFile = File(...)
, fileExtension: str = Form(...)):
if(fileExtension == 'txt'):
raw_txt = readTxt(file)
raw_txt = raw_txt.decode()
return raw_txt
def readTxt(file):
return file.read()
This code above throws internal server error. Can I know how to correct it?
Logs:
File "/home/readerProject/.venv/lib/python3.7/site-packages/fastapi/routing.py", line 183, in app dependant=dependant, values=values, is_coroutine=is_coroutine File "/home/readerProject/.venv/lib/python3.7/site-packages/fastapi/routing.py", line 133, in run_endpoint_function return await dependant.call(**values)
File "/home/readerProject/api.py", line 28, in upload_file_and_read raw_txt = raw_txt.decode()
AttributeError: 'coroutine' object has no attribute 'decode' /home/readerProject/.venv/lib/python3.7/site-packages/uvicorn/protocols/http/h11_impl.py:396: RuntimeWarning: coroutine 'UploadFile.read' was never awaited
self.transport.close() RuntimeWarning: Enable tracemalloc to get the object allocation traceback
raw_txt = await raw_txt.decode()instead ofraw_txt = raw_txt.decode()?