I am learning FastAPI and trying to upload an image file to the server, and then I want to access it by URK in the frontend.
For example, I send a POST request with file image.png
, and then using a GET
request I should get a response including the link to the uploaded file.
Response:
{
"filename": "image.png"
"url": "127.0.0.1/image.png"
}
This is my code so far:
from fastapi import FastAPI, UploadFile, File
app = FastAPI()
@app.post("/")
async def root(file:UploadFile = File(...)):
return {"filename":file.filename}
@app.get("/get")
async def root():
return {
"filename":file.filename,
"url":"127.0.0.1/"+file.filename
}
But it doesn't work.