Implement basic post request with body

This commit is contained in:
Curt Spark 2024-04-16 17:36:32 +01:00
parent cf83399182
commit 2871e5fc65
3 changed files with 30 additions and 2 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.venv
__pycache__

29
main.py
View File

@ -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!"}

1
run.sh Executable file
View File

@ -0,0 +1 @@
uvicorn main:app --reload