GET 작업

FastAPI 입문

Matt Eckerle

Software and Data Engineering Leader

GET 작업 복습

HTTP 프로토콜 – 여러 작업 유형

  • GET이 가장 흔함

예: https://www.google.com:80/search?q=fastapi

GET 요청의 핵심 구성:

  • 호스트(Host), 예: www.google.com
  • 포트(Port), 예: 80(기본값)
  • 경로(Path), 예: /search
  • 쿼리 문자열(Query string), 예: ?q=fastapi
FastAPI 입문

FastAPI GET 작업

가장 단순한 FastAPI 애플리케이션:

from fastapi import FastAPI

# 앱 인스턴스화 app = FastAPI()
# 루트에 대한 GET 요청 처리 @app.get("/") def root(): return {"message": "Hello World"}
1 https://fastapi.tiangolo.com/tutorial/first-steps/
FastAPI 입문

cURL 웹 클라이언트 사용

주요 cURL 옵션:

$ curl -h
Usage: curl [options...] <url>
 -v, --verbose               Make the operation more talkative
 -H, --header <header/@file> Pass custom header(s) to server
 -d, --data <data>           HTTP POST data

사용 예시:

$ curl http://localhost:8000
{"message":"Hello World"}
FastAPI 입문

쿼리 매개변수

새 엔드포인트:

  • 경로: "/hello"
  • 쿼리 매개변수: "name"
    • 기본값: "Alan"
@app.get("/hello")
def hello(name: str = "Alan"):
    return {"message": f"Hello {name}"}

요청에 이름 없음: 이름 없이 curl 요청한 터미널. 응답은 "Hello Alan."

요청에 이름 포함: 이름을 포함해 curl 요청한 터미널. 응답은 "Hello Steve."

FastAPI 입문

연습해 봅시다!

FastAPI 입문

Preparing Video For Download...