From 2871e5fc65f5cd7c7ef7c942451b8ea008621a22 Mon Sep 17 00:00:00 2001 From: cspark Date: Tue, 16 Apr 2024 17:36:32 +0100 Subject: [PATCH] Implement basic post request with body --- .gitignore | 2 ++ main.py | 29 +++++++++++++++++++++++++++-- run.sh | 1 + 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100755 run.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0e5ac79 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.venv +__pycache__ \ No newline at end of file diff --git a/main.py b/main.py index cb7d2d2..89c9ced 100644 --- a/main.py +++ b/main.py @@ -1,14 +1,39 @@ from typing import Union from fastapi import FastAPI +from fastapi.middleware.cors import CORSMiddleware + +from pydantic import BaseModel app = FastAPI() +origins = [ + "http://localhost", + "http://localhost:8080", +] + +app.add_middleware( + CORSMiddleware, + allow_origins=origins, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) @app.get("/") -def root(): +def getroot(): return {"Hello": "World"} +class ApiBody(BaseModel): + username: str + password: str + +@app.post("/api") +def postapi(body: ApiBody): + print(body.username) + print(body.password) + return body + @app.get("/api") -def root(): +def getapi(): return {"Hello": "API!"} diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..539bcc2 --- /dev/null +++ b/run.sh @@ -0,0 +1 @@ +uvicorn main:app --reload