Home > Software design >  uploading flie to FastAPI endpoint using curl - 307 Temporary Redirect
uploading flie to FastAPI endpoint using curl - 307 Temporary Redirect

Time:10-05

I have a fastAPI endpoint that recieves a file and saves it to disk as follows:

from fastapi import FastAPI, File, UploadFile
import shutil

app = FastAPI()

@app.post('/upload')
async def upload_file(file: UploadFile=File(...)):
    with open(file.filename, "wb") as buffer:
        shutil.copyfileobj(file.file, buffer)

    return {
        "filename": file.filename,
    }

This works as expected when I upload a file through the docs interface at http://localhost:8000/docs I am able to select a file and it successfully uploads.

However, attempting the same with curl fail:

curl -X POST localhost:8000/upload -F [email protected]

the curl command returns nothing and on the server side a 307 Temporary Redirect is logged.

I am not sure what I am missing here

CodePudding user response:

In some scenario/setup where FastAPI redirect the request, use the curl with full dns/ip address.

something like this :

curl -X 'POST' '127.0.0.1:8000/upload' -F '[email protected]

or can add headers(-H) too based on the application that is built.

curl -X 'POST' \
  'http://127.0.0.1:8000/upload' \
  -H 'accept: application/json' \
  -H 'Content-Type: multipart/form-data' \
  -F '[email protected];type=application/json'
  • Related