0%

FastAPI自学教程(52) - 自定义响应机制

1.基础响应类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from fastapi import FastAPI
from fastapi.responses import JSONResponse, HTMLResponse

app = FastAPI()

@app.get("/custom", response_class=JSONResponse)
async def custom_response():
return JSONResponse(content={"message": "自定义JSON"})

@app.get("/html", response_class=HTMLResponse)
async def html_page():
return """
<html><body><h1>Hello HTML</h1></body></html>
"""
  • 通过response_class参数指定响应类型(默认JSONResponse
  • 支持类型包括:
    • JSONResponse:默认JSON格式,支持ORJSONResponse提升性能(需安装orjson库)
    • HTMLResponse:返回HTML内容
    • PlainTextResponse:纯文本格式
    • FileResponse:文件下载
  • 请求处理流程:
    graph TD
      A[客户端请求] --> B{响应类型判断}
      B -->|JSON| C[自动序列化数据]
      B -->|HTML| D[渲染HTML内容]
      B -->|文件| E[流式传输]

2.流式响应与文件处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from fastapi.responses import StreamingResponse, FileResponse

def generate_large_file():
for i in range(1000):
yield f"数据块 {i}\n"

@app.get("/stream")
async def stream_data():
return StreamingResponse(
generate_large_file(),
media_type="text/plain",
headers={"Content-Disposition": "attachment; filename=bigfile.txt"}
)

@app.get("/download/{file_name}")
async def download(file_name: str):
return FileResponse(
path=f"/data/{file_name}",
filename="download.zip",
media_type="application/octet-stream"
)
  • StreamingResponse适合大文件传输,避免内存溢出
  • FileResponse自动处理范围请求(Range requests)
  • 关键参数:
    • media_type:指定MIME类型
    • headers:设置下载头信息

3.响应头与Cookie控制

3.1 自定义响应头

1
2
3
4
5
6
7
from fastapi import Response

@app.get("/headers")
async def set_headers(response: Response):
response.headers["X-Custom-Header"] = "value"
response.headers["Cache-Control"] = "max-age=3600"
return {"status": "headers set"}

3.2 Cookie操作

1
2
3
4
5
6
7
8
from fastapi.responses import JSONResponse

@app.post("/login")
async def login():
content = {"message": "登录成功"}
response = JSONResponse(content=content)
response.set_cookie(key="token", value="abc123", max_age=3600)
return response
  • 通过Response对象直接操作头信息和Cookie
  • 安全建议:
    • 敏感Cookie设置httponly=True
    • 生产环境启用secure=True

4.高级配置技巧

4.1 性能优化

1
2
3
@app.get("/fast-json", response_class=ORJSONResponse)
async def optimized_response():
return {"data": [i for i in range(10000)]}
  • 使用ORJSONResponse替代默认JSON解析器,提升3-5倍序列化速度
  • 避免在响应类中做复杂数据转换

4.2 媒体类型覆盖

1
2
3
4
@app.get("/xml-data", response_class=Response)
async def xml_response():
content = """<?xml version="1.0"?><data><item>test</item></data>"""
return Response(content=content, media_type="application/xml")
  • 通过media_type参数强制指定响应格式
  • 支持自定义MIME类型(如application/vnd.ms-excel

5.生产环境注意事项

  1. 安全头配置
    1
    2
    3
    4
    response.headers.update({
    "Strict-Transport-Security": "max-age=31536000",
    "Content-Security-Policy": "default-src 'self'"
    })
  2. 文件传输
    • 大文件使用分块传输编码
    • 限制文件下载目录访问权限
  3. 性能监控
    • 添加X-Response-Time头记录处理耗时
    • 使用APM工具跟踪响应延迟

测试命令示例:

1
2
3
4
5
# 测试流式响应
curl http://localhost:8000/stream --output largefile.txt

# 测试头信息
curl -i http://localhost:8000/headers