4

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".

icedwater
  • 4,701
  • 3
  • 35
  • 50
TMikonos
  • 359
  • 1
  • 11
  • 29
  • add the complete error traceback to the OP – JPG Nov 23 '20 at 17:10
  • Did you find a solution to this? – Jubick Mar 15 '21 at 20:55
  • 1
    Yes, it was a bug in the postman. I created a client for this code and it worked. I forgot to update it. – TMikonos Mar 16 '21 at 10:33
  • @TMikonos could you post the solution below, please? :) – icedwater Feb 06 '23 at 03:42
  • @icedwater Please have a look at [this answer](https://stackoverflow.com/a/70657621/17865804) and [this answer](https://stackoverflow.com/a/70667530/17865804) on how to upload files in FastAPI. – Chris Feb 06 '23 at 12:30
  • Thanks @Chris, this isn't my question. I just commented because I was cleaning up some related FastAPI questions and OP mentioned they already had a solution, but nothing was posted here. – icedwater Feb 07 '23 at 06:57

0 Answers0