1

I'm trying to upload a video to FastAPI, however, an error occurs.

This is my python code.

from fastapi import FastAPI, File, UploadFile
from typing import List
import os

app = FastAPI()

@app.get("/")
def read_root():
  return { "Hello": "World" }

@app.post("/files/")
async def create_files(files: List[bytes] = File(...)):
    return {"file_sizes": [len(file) for file in files]}

@app.post("/uploadfiles")
async def create_upload_files(files: List[UploadFile] = File(...)):
    print('here')
    UPLOAD_DIRECTORY = "./"
    for file in files:
        contents = await file.read()
        with open(os.path.join(UPLOAD_DIRECTORY, file.filename), "wb") as fp:
            fp.write(contents)
        print(file.filename)
    return {"filenames": [file.filename for file in files]}

When I use Postman, the image is uploaded fine, but the video is not.

This is the error:

INFO:     127.0.0.1:57251 - "POST /uploadfiles/ HTTP/1.1" 307 Temporary Redirect
Did not find boundary character 254 at index 2
INFO:     127.0.0.1:57252 - "POST /uploadfiles HTTP/1.1" 400 Bad Request

Please tell me what is the problem.

Chris
  • 18,724
  • 6
  • 46
  • 80
Deque
  • 21
  • 2
  • What does your upload request look like? – MatsLindh Apr 08 '22 at 13:17
  • The `307 Temporary Redirect` status code is caused due to appending `/` at the end of the path, while the path defined for your endpoint is `/uploadfiles` (see [here](https://stackoverflow.com/a/70354027)). The `400 Bad Request` is likely an issue with Postman, when uploading big files, as described [here](https://stackoverflow.com/q/64972165) and [here](https://github.com/tiangolo/fastapi/issues/2401), hence the [error](https://github.com/andrew-d/python-multipart/blob/master/multipart/multipart.py#L1178). Try using `requests`, as shown [here](https://stackoverflow.com/a/70657621/17865804). – Chris Apr 08 '22 at 15:47

0 Answers0