minTwin
minTwin

Reputation: 1395

name 'File' is not define when used with FASTAPI

The following function should output the file size of the image when sending a Post request from Insomnia/Postman API development environments.

@app.post("/user/photo")
async def upload_user_photo(response: Response, profile_photo: bytes = File(...)):
    response.headers["x-file-size"] = str(len(profile_photo))
    response.set_cookie(key="cookie-api", value="test")
    return {"file size": len(profile_photo)}

However the following error occurs:

  File ".\run.py", line 54, in <module>
    async def upload_user_photo(response: Response, profile_photo: bytes = File(...)):
NameError: name 'File' is not defined

I've read that File() has been depreciated from Python 3, so what would be a way to get this to work?

Upvotes: 0

Views: 645

Answers (1)

Tim Roberts
Tim Roberts

Reputation: 54620

The Python file function was deprecated. Remember that capitalization matters. What that code wants is the File class from fastapi. You need to add:

from fastapi import File

Upvotes: 4

Related Questions