question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Upload file gives body error with UploadFile

See original GitHub issue

Description

I am having issues uploading a file to a FastAPI server. I found similar issues: #1367 and #405

However, there was no solution. The issue is that it works with small sized files, but when a file is bigger than 100Kb it does not work. It looks like the reading of the file does not finish before trying to run the other tasks like writing.

Code sample

import uvicorn
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import FileResponse
import aiofiles

app = FastAPI(title='Untitled', description='doThings', 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(await file.read())
    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')

Output

The error happens just when it tries to read the file. I have tried it only with the return and it does not work, neither. I have run many tests and I don’t get where the error comes.

This is the output of the uvicorn server:

INFO:     Started server process [7108]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:5003 (Press CTRL+C to quit)
INFO:     127.0.0.1:52854 - "POST /api/upload HTTP/1.1" 307 Temporary Redirect
Did not find boundary character 80 at index 2
INFO:     127.0.0.1:52855 - "POST /api/upload/ HTTP/1.1" 400 Bad Request
WARNING:  Invalid HTTP request received.

And in Postman I get this error message:

{
    "detail": "There was an error parsing the body"
}

Environment

OS: Windows 10 Python: Python 3.8.6 FastApi 0.61.2 Starlette 0.13.6 Pydantic 1.7.2

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
llorencscommented, Nov 30, 2020

Hello, following this, I have tested it directely with a requests snippet and it worked correctly. So, it can be closed.

0reactions
bouncommented, Jul 27, 2022

Sorry for commenting something totally unrelated to a closed bug, but google brought me here.

I had the same error, but did not use postman. In my case it was a kubernetes deployment. The solution was that the helm chart set the root fs to readonly.

Hope someone finds this helpful in the future.

Read more comments on GitHub >

github_iconTop Results From Across the Web

File upload using FastAPI returns error 422 - Stack Overflow
On client side you should use the key you gave on server side. In your case, that is data . Thus, you should...
Read more >
Request Files - FastAPI
This is because uploaded files are sent as "form data". Import File ¶. Import File and UploadFile from fastapi :.
Read more >
Upload file data | Google Drive
A resumable upload lets you resume an upload operation after a communication failure interrupts the flow of data. Because you don't have to...
Read more >
File upload | NestJS - A progressive Node.js framework
To handle file uploading, Nest provides a built-in module based on the multer ... A factory which receives the error message and returns...
Read more >
Primefaces FileUpload Component Example Tutorial
HTML provides you file input tag to select the file, but we need a lot more to upload a file to the server....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found