I have this FastAPI application
import uvicorn
from fastapi import FastAPI
from starlette.responses import FileResponse
app = FastAPI()
@app.get("/a")
async def read_index():
return FileResponse('static/index.html')
@app.get("/a/b")
def download():
return "get"
@app.post("/a/b")
def ab():
return "post"
def main():
uvicorn.run("run:app", host="0.0.0.0", reload=True, port=8001)
if __name__ == "__main__":
main()
and in static/index.html
I have:
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<section>
<form method="post" action="/a/b" enctype="multipart/form-data">
<div>
<p>Download:</p>
</div>
<div>
<input type="submit" value="Download"/>
</div>
</form>
</section>
</body>
</html>
I send a get request to http://127.0.0.1:8001/a
and click on the download
button it loads, it returns "get"
However, I change the application to
import uvicorn
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
app = FastAPI()
app.mount("/a", StaticFiles(directory="static", html=True))
@app.get("/a/b")
def download():
return "get"
@app.post("/a/b")
def ab():
return "post"
def main():
uvicorn.run("run:app", host="0.0.0.0", reload=True, port=8001)
if __name__ == "__main__":
main()
with the same HTML file, I click on the download button, and I get detail: "Method Not Allowed"
because it is doing INFO: 127.0.0.1:58109 - "POST /b HTTP/1.1" 405 Method Not Allowed
I want to use mount becuase I want to put js and css files there as well, to be used by the index.html file. how should I do this?