I am trying to create an endpoint for uploading files. However, if the file is bigger than 100Kb I am getting an error of 400 Bad Request. My guess is that it does not have enough time to upload the file.
The code of my function is like this:
import uvicorn
from fastapi import FastAPI, File, UploadFile
import pathlib
app = FastAPI(title='Tool', description='Tool descr', version='0.0.1')
uploads = 'uploads'
uploads_dir = pathlib.Path(os.getcwd(), uploads)
@app.post('/api/upload/')
async def upload_file(file: UploadFile=File(...)):
"""
Upload the file to be processed.
"""
print(file)
async with aiofiles.open(file.file, 'rb') as fin:
exfile = fin.read()
file_name = pathlib.Path(uploads_dir, file.filename)
async with aiofiles.open(f'{file_name}', 'wb') as f:
await f.write(exfile)
return {'filename': file.filename,
'content-type': file.content_type}
if __name__ == '__main__':
uvicorn.run('main:app', host='127.0.0.1', port=5003, log_level='info')
So the server gives me a Bad Request answer with did not find CR at end of boundary
if is a text file or if it is a binary file Did not find boundary character 198 at index 2
, for example.
Error in Postman:
{
"detail": "There was an error parsing the body"
}
The error that the server is returning is basically this:
Did not find boundary character 145 at index 2
INFO: 127.0.0.1:58032 - "POST /api/upload/ HTTP/1.1" 400 Bad Request
WARNING: Invalid HTTP request received.
INFO: 127.0.0.1:58033 - "POST /api/upload HTTP/1.1" 307 Temporary Redirect
<starlette.datastructures.UploadFile object at 0x000001CCCF62AE80>
INFO: 127.0.0.1:58033 - "POST /api/upload/ HTTP/1.1" 200 OK
INFO: 127.0.0.1:58156 - "POST /api/upload HTTP/1.1" 307 Temporary Redirect
<starlette.datastructures.UploadFile object at 0x000001CCCF62E7F0>
INFO: 127.0.0.1:58156 - "POST /api/upload/ HTTP/1.1" 200 OK
INFO: 127.0.0.1:58190 - "POST /api/upload HTTP/1.1" 307 Temporary Redirect
Did not find CR at end of boundary (54)
INFO: 127.0.0.1:58191 - "POST /api/upload/ HTTP/1.1" 400 Bad Request
These are a few requests, some were successful (approx 50k of size of the files) and the others were not processed as a "Bad Request".