본문 바로가기
프로그래머/프로그래밍

FastAPI 사용법 & 예제

by plog 2023. 6. 16.

■ FastAPI 설치

pip install fastapi
pip install uviconrn

* Uvicorn이란 uvloop 및 httptools를 사용하는 ASGI web server

 

FastAPI 파일 생성

파일명: fastapi_test.py

from fastapi import FastAPI

api = FastAPI()

@api.get("/")
def root():
    return {"message":"Hello Lee"}  # key -value 형태의 딕셔너리...
    
# 파라미터 
@api.get('/hello')
def hello(name):
    result_str = 'Hello. ' + name

    return result_str

Uvicorn 서버 실행

 

터미널에서 명령어 입력

uvicorn fastapi_test:api --reload

자세한 사용법

 

결과 확인

 

API 문서

http://127.0.0.1:8000/redoc 

 

 FastAPI 공식문서

https://fastapi.tiangolo.com/ko/

 

댓글