0

I defined this endpoint using FastAPI:

@app.post("/detect/")
def predict(request: Request,  file: bytes = File(...)):

    return f"Filename is {file.name}"

and I am testing that endpoint this way:

url = "http://127.0.0.1:8000/detect"
file = 'myimage.jpg'
f = open(file, 'rb')
files = {"file": (f.name, f, "multipart/form-data")}   

response = requests.request("POST", url,  files=files)

print(response.text)

So, I would like the response to be "Filename is myimage.jpg", but file.name is not defined. Which is the correct way to get the file name inside the endpoint? I know I can use UploadFile instead of bytes, but in this particular case, I would like to keep bytes because of other operations I need to do inside the endpoint.

Laura
  • 1,192
  • 2
  • 18
  • 36
  • Is this what are you searching https://stackoverflow.com/questions/53143852/set-filename-on-post-with-python-requests ? – Gonzalo Odiard Sep 30 '21 at 20:16
  • You're typing `file` as `bytes`. Instead type it as `UploadFile` as shown in https://fastapi.tiangolo.com/tutorial/request-files/#file-parameters-with-uploadfile – MatsLindh Sep 30 '21 at 21:19
  • Only `UploadFile` allows you to access file metadata – alex_noname Sep 30 '21 at 21:20
  • Does this answer your question? [How to Upload File using FastAPI?](https://stackoverflow.com/questions/63048825/how-to-upload-file-using-fastapi) – Chris Oct 29 '22 at 12:31

0 Answers0