Skip to content

I wanted to record the request result, but an error occurred while retrieving the content #9804

@findmio

Description

@findmio

Privileged issue

  • I'm @tiangolo or he asked me directly to create an issue here.

Issue Content

I use fastapi 0.99.1

Here is my custom middleware

import time

from fastapi import Request
from starlette.middleware.base import BaseHTTPMiddleware
from uvicorn.protocols.utils import get_path_with_query_string
from starlette.responses import StreamingResponse
from app.core.logger import access_logger as logger


class AccessLogMiddleware(BaseHTTPMiddleware):
    async def dispatch(self, request: Request, call_next):
        start_time = time.perf_counter_ns()

        response: StreamingResponse = await call_next(request)

        end_time = time.perf_counter_ns()

        process_time = round((end_time - start_time) / 10**9, 3)

        status_code = response.status_code
        path = get_path_with_query_string(request.scope)
        client_host = request.client.host
        client_port = request.client.port
        http_method = request.method
        content = response.content

        logger.info(
            f"{http_method} | status_code: {status_code} | {path} | time: {process_time} s | {client_host}:{client_port} | content: {content}")

        return response

An exception will be thrown here:'_StreamingResponse' object has no attribute 'content'

I would like to ask how to solve it, thank you

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions