from fastapi import FastAPI, Header, HTTPException app = FastAPI() @app.get("/secure-data") async def secure_data(api_key: str = Header(None)): if api_key != "my_secure_token": raise HTTPException(status_code=401, detail="無效的 API 金鑰") return {"message": "驗證成功,提供安全數據"} # 啟動伺服器後,使用 cURL 測試: # curl -H "api-key: my_secure_token" http://127.0.0.1:8000/secure-data