Implement basic post request with body
This commit is contained in:
parent
cf83399182
commit
2871e5fc65
|
|
@ -0,0 +1,2 @@
|
|||
.venv
|
||||
__pycache__
|
||||
29
main.py
29
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!"}
|
||||
|
|
|
|||
Loading…
Reference in New Issue