FastAPI 入门
Matt Eckerle
Software and Data Engineering Leader
HTTP 协议:多种操作类型
示例:https://www.google.com:80/search?q=fastapi
GET 请求的关键部分:
www.google.com80(默认)/search?q=fastapi最简单的 FastAPI 应用:
from fastapi import FastAPI# 实例化应用 app = FastAPI()# 处理根路径的 GET 请求 @app.get("/") def root(): return {"message": "Hello World"}
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"}
新增端点:
@app.get("/hello")
def hello(name: str = "Alan"):
return {"message": f"Hello {name}"}
请求中未带姓名:

请求中带姓名:

FastAPI 入门