개발로 자기계발
article thumbnail
728x90

1. dictionary 정의

BOOKS = {
    'book_1': {'title':'Title One','author':'Author One'},
    'book_2': {'title':'Title Two','author':'Author Two'},
    'book_3': {'title': 'Title Three', 'author': 'Author Three'},
    'book_4': {'title': 'Title Four', 'author': 'Author Four'},
    'book_5': {'title': 'Title Five', 'author': 'Author Five'},
}

 

2. 함수 정의

  • 전체 BOOKS의 list를 볼 수 있음
@app.get("/")
async def read_all_books():
    return BOOKS
  • BOOKS의 1개의 키 값이 무조건 들어가야함
  • read_book의 매개변수에는 string만 들어갈 수 있음
@app.get("/{book_name}")
async def read_book(book_name: str):
    return BOOKS[book_name]
  • dictionary에 없는 키 값을 입력 시 에러
 
728x90
SMALL

'Develop > FastAPI' 카테고리의 다른 글

FastAPI Post Request - 09  (0) 2022.12.22
FastAPI Query Parameters - 08  (0) 2022.12.22
FastAPI Path Parameters(열거형) - 06  (0) 2022.12.21
FastAPI 요청 메서드 로직 - 05  (0) 2022.12.21
FastAPI Swagger / HTTP 데코레이터 - 04  (0) 2022.12.21
profile

개발로 자기계발

@김잠봉

틀린부분이나 조언이 있다면 언제든 환영입니다:-)