How to upload a single file to FastAPI server using CURL

I’m trying to set up a FastAPI server that can receive a single file upload from the command line using curl.

I’m following the FastAPI Tutorial here:

from typing import List
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import HTMLResponse
app = FastAPI()"/file/")
async def create_file(file: bytes = File(...)):
     return {"file_size": len(file)}"/uploadfile/")
async def create_upload_file(file: UploadFile = File(...)):
    return {"filename": file.filename}"/files/")
async def create_files(files: List[bytes] = File(...)):
    return {"file_sizes": [len(file) for file in files]}"/uploadfiles/")
async def create_upload_files(files: List[UploadFile] = File(...)):
    return {"filenames": [file.filename for file in files]}

Running this code and then opening “” in a browser gives me a upload form with four ways of selecting files and uploading

I followed this tutorial:

I tried uploading a file “1.json” in the current directory like this

curl -F "file=@1.json"

on the server side I get this result

INFO: - "POST /uploadfiles HTTP/1.1" 307 Temporary Redirect

I do not understand why a redirect happens.

I need help on how to either guess the correct curl syntax or fix this on the FastAPI side.


The solution was to tell curl to follow a redirect.

curl -L -F "file=@1.json"

which then uploads the file.