Nhập môn FastAPI
Matt Eckerle
Software and Data Engineering Leader
Giao thức HTTP - nhiều loại thao tác
Ví dụ: https://www.google.com:80/search?q=fastapi
Các phần chính của một yêu cầu GET:
www.google.com80 (mặc định)/search?q=fastapiỨng dụng FastAPI tối giản:
from fastapi import FastAPI# Khởi tạo app app = FastAPI()# Xử lý GET tới root @app.get("/") def root(): return {"message": "Hello World"}
Tùy chọn cURL chính:
$ 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
Ví dụ dùng:
$ curl http://localhost:8000
{"message":"Hello World"}
Endpoint mới:
@app.get("/hello")
def hello(name: str = "Alan"):
return {"message": f"Hello {name}"}
Không có name trong yêu cầu:

Có name trong yêu cầu:

Nhập môn FastAPI