1. get방식
get방식은 파라미터를 추가하는 것으로 데이터를 받을 수 있다.
파라미터에 작성되는 변수명은 쿼리스트링의 'name'과 동일하게 작성하면 된다.
from fastapi import FastAPI, Form
import uvicorn
app = FastAPI()
@app.get("/")
async def root():
return 'welcome!'
@app.get("/get")
async def get(a):
return {"param" : a}
if __name__ == "__main__":
uvicorn.run(app, host="localhost", port="80")
2. post 방식
post 방식 또한 파라미터를 추가하면 된다.
하지만 get과 다르게 변수를 From() 객체로 초기화하여 데이터를 처리한다.
변수명이 데이터의 'name'과 동일해야하는 것은 get방식과 똑같다.
from fastapi import FastAPI, Form
import uvicorn
app = FastAPI()
@app.get("/")
async def root():
return 'welcome!'
@app.post("/post")
async def post(b: str = Form()):
return {"post" : b}
if __name__ == "__main__":
uvicorn.run(app, host="localhost", port="80")
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Post 방식</title>
</head>
<body>
<form action="http://localhost/post" method="post">
<input type="text" name="b" value="200"/>
<input type="submit" value="포스트로 보내기"/>
</form>
</body>
</html>
'Python' 카테고리의 다른 글
FastAPI 리다이렉트 / 포워딩 (0) | 2023.01.05 |
---|---|
html에서 파이썬 변수 사용하기 (0) | 2023.01.05 |
파이썬 FastAPI 첫 시작 (1) | 2023.01.04 |
파이썬 sqlite 연동 (0) | 2023.01.04 |
파이썬 mssql 연동 (0) | 2023.01.03 |