FastAPI Post Request - 09

2022. 12. 22. 09:15·Develop/FastAPI
728x90
SMALL

1. POST

  • 데이터 생성
  • DB 저장

​

2. 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'},
}

 

3. 함수 정의

@app.post("/")
async def create_book(book_title, book_author):
    current_book_id = 0

    if len(BOOKS) > 0:
        for book in BOOKS:
            x = int(book.split('_')[-1])
            if x > current_book_id:
                current_book_id = x
    BOOKS[f'book_{current_book_id + 1}'] = {'title':book_title, 'author': book_author}
    return BOOKS[f'book_{current_book_id + 1}']
  • current_book_id 변수 선언 후 0 선언
  • BOOKS의 길이가 0이 아닐 때 if 문 실행
  • x는 기존 BOOKS의 딕셔너리 키 값의 뒤에 숫자를 가져옴
  • x가 current_book_id보다 클 시 if 문 실행
  • current_book_id에 x을 다시 담아줌 ex) 1, 2, 3, 4, 5
  • BOOKS dictionary에 마지막에 온 숫자 5에 +1을 더해서 6으로 삽입
  • 최종 dicitonary 형태는 book_6가 추가된 형태

​

4. docs로 6번째 dict 추가 후 전체 리스트 확인

  • 전체 리스트를 뽑아봤을 때 book_6이 추가가 된 걸 확인 할 수 있음

 

728x90
SMALL
저작자표시 비영리 변경금지 (새창열림)

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

FastAPI Delete Request - 11  (0) 2022.12.22
FastAPI Put Request - 10  (0) 2022.12.22
FastAPI Query Parameters - 08  (0) 2022.12.22
FastAPI Path Parameters(추가) - 07  (0) 2022.12.21
FastAPI Path Parameters(열거형) - 06  (0) 2022.12.21
'Develop/FastAPI' 카테고리의 다른 글
  • FastAPI Delete Request - 11
  • FastAPI Put Request - 10
  • FastAPI Query Parameters - 08
  • FastAPI Path Parameters(추가) - 07
동석해요
동석해요
공부하고 싶은게 많은, 사소한 IT 지식들 공유
    250x250
  • 동석해요
    개발로 자기계발
    동석해요
  • 전체
    오늘
    어제
    • 분류 전체보기 (226)
      • Develop (126)
        • 기초지식 (12)
        • FastAPI (102)
        • Django (11)
      • Database & Data (62)
        • 기초지식 (16)
        • MySQL (29)
        • PostgreSQL (8)
        • 데이터 분석 (9)
      • 인공지능 (11)
        • PyTorch (9)
      • Cloud (8)
        • AWS (4)
        • GCP (2)
      • 버그처리 (14)
      • 회고 & 일상 (5)
  • 인기 글

  • 최근 글

  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.3
동석해요
FastAPI Post Request - 09
상단으로

티스토리툴바