Upload file gives body error with UploadFile
See original GitHub issueDescription
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:
[32mINFO[0m: Started server process [[36m7108[0m]
[32mINFO[0m: Waiting for application startup.
[32mINFO[0m: Application startup complete.
[32mINFO[0m: Uvicorn running on [1mhttp://127.0.0.1:5003[0m (Press CTRL+C to quit)
[32mINFO[0m: 127.0.0.1:52854 - "[1mPOST /api/upload HTTP/1.1[0m" [33m307 Temporary Redirect[0m
Did not find boundary character 80 at index 2
[32mINFO[0m: 127.0.0.1:52855 - "[1mPOST /api/upload/ HTTP/1.1[0m" [31m400 Bad Request[0m
[33mWARNING[0m: 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:
- Created 3 years ago
- Comments:6 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hello, following this, I have tested it directely with a requests snippet and it worked correctly. So, it can be closed.
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.