1

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.

Chris
  • 18,724
  • 6
  • 46
  • 80
Kreikten
  • 11
  • 1
  • Have a look at [this answer](https://stackoverflow.com/a/70657621/17865804) as well on how to upload and save a file in FastAPI. – Chris Nov 24 '22 at 11:19

0 Answers0