我已经阅读了 FastAPI 关于中间件的文档(特别是中间件教程、CORS 中间件部分和高级中间件指南),但找不到如何编写可以使用该add_middleware函数添加的中间件类的具体示例(相反到使用装饰器添加的基本中间件功能),也不在此站点上。
我更喜欢使用add_middleware基于应用程序的装饰器的原因是,我想在共享库中编写一个中间件,该库将由多个不同的项目使用,因此我无法将其绑定到特定实例FastAPI。
所以我的问题是:你是如何做到的?
我们正在使用 Python FastAPI 编写一个 Web 服务,该服务将托管在 Kubernetes 中。出于审计目的,我们需要保存特定路由的request/的原始 JSON 正文。JSON的主体大小约为1MB,最好这不应该影响响应时间。我们怎样才能做到这一点?responserequestresponse
我有一个 FastAPI 应用程序,我希望将默认日志写入 STDOUT,并使用 JSON 格式的以下数据:
应用程序日志应如下所示:
{
"XYZ": {
"log": {
"level": "info",
"type": "app",
"timestamp": "2022-01-16T08:30:08.181Z",
"file": "api/predictor/predict.py",
"line": 34,
"threadId": 435454,
"message": "API Server started on port 8080 (development)"
}
}
}
Run Code Online (Sandbox Code Playgroud)
访问日志应如下所示:
{
"XYZ": {
"log": {
"level": "info",
"type": "access",
"timestamp": "2022-01-16T08:30:08.181Z",
"message": "GET /app/health 200 6ms"
},
"req": {
"url": "/app/health",
"headers": {
"host": "localhost:8080",
"user-agent": "curl/7.68.0",
"accept": "*/*"
},
"method": "GET",
"httpVersion": "1.1",
"originalUrl": "/app/health",
"query": {}
},
"res": {
"statusCode": 200,
"body": …Run Code Online (Sandbox Code Playgroud) 我尝试为 FastAPI 编写一个简单的中间件来查看响应主体。
在这个例子中,我只记录正文内容:
app = FastAPI()
@app.middleware("http")
async def log_request(request, call_next):
logger.info(f'{request.method} {request.url}')
response = await call_next(request)
logger.info(f'Status code: {response.status_code}')
async for line in response.body_iterator:
logger.info(f' {line}')
return response
Run Code Online (Sandbox Code Playgroud)
但是看起来我以这种方式“消耗”了身体,导致了这个异常:
...
File ".../python3.7/site-packages/starlette/middleware/base.py", line 26, in __call__
await response(scope, receive, send)
File ".../python3.7/site-packages/starlette/responses.py", line 201, in __call__
await send({"type": "http.response.body", "body": b"", "more_body": False})
File ".../python3.7/site-packages/starlette/middleware/errors.py", line 156, in _send
await send(message)
File ".../python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py", line 515, in send
raise RuntimeError("Response content shorter than Content-Length")
RuntimeError: Response content shorter than …Run Code Online (Sandbox Code Playgroud) 我正在尝试从 FastAPI 后端下载一个大文件 ( .tar.gz)。在服务器端,我只是验证文件路径,然后Starlette.FileResponse返回整个文件\xe2\x80\x94,就像我在 StackOverflow 上的许多相关问题中看到的那样。
服务器端:
\nreturn FileResponse(path=file_name, media_type=\'application/octet-stream\', filename=file_name)\nRun Code Online (Sandbox Code Playgroud)\n之后,我收到以下错误:
\n File "/usr/local/lib/python3.10/dist-packages/fastapi/routing.py", line 149, in serialize_response\n return jsonable_encoder(response_content)\n File "/usr/local/lib/python3.10/dist-packages/fastapi/encoders.py", line 130, in jsonable_encoder\n return ENCODERS_BY_TYPE[type(obj)](obj)\n File "pydantic/json.py", line 52, in pydantic.json.lambda\nUnicodeDecodeError: \'utf-8\' codec can\'t decode byte 0x8b in position 1: invalid start byte\nRun Code Online (Sandbox Code Playgroud)\n我也尝试使用StreamingResponse,但得到了同样的错误。还有其他方法可以做到吗?
我的代码中的StreamingResponse:
File "/usr/local/lib/python3.10/dist-packages/fastapi/routing.py", line 149, in serialize_response\n return jsonable_encoder(response_content)\n File "/usr/local/lib/python3.10/dist-packages/fastapi/encoders.py", line 130, in jsonable_encoder\n return ENCODERS_BY_TYPE[type(obj)](obj)\n File …Run Code Online (Sandbox Code Playgroud) 我是 webdev 的新手,我有这样的用例:用户向 API 发送一个大文件(例如视频文件),然后该文件需要可供其他 API 访问(可能位于不同的服务器上) )进行进一步处理。
我使用 FastAPI 作为后端,定义一个类型为 的文件参数UploadFile来接收和存储文件。但是,让其他 API 可以访问此文件的最佳方法是什么?有没有办法可以URL从保存的文件中获取公开访问权限,其他 API 可以使用哪些文件来下载该文件?