【问题标题】:Field does not add into header in quart / flask middleware字段不添加到夸脱/烧瓶中间件的标题中
【发布时间】:2021-12-21 08:56:32
【问题描述】:

我正在尝试在 Flask 中间件中记录数据。我使用process_response 函数来记录响应数据。

我有这个代码块,我试图将持续时间注入响应的标头。但是 X-Time-Taken 没有被添加到标题中。响应头是request header: Content-Type: text/plain; charset=utf-8。

为什么X-Time-Taken 没有被添加到标题中?

仅供参考,我的代码基于 this,这表明我正在做的事情应该有效。

class SampleWSGI():    
    def __init__(self, app, config=None):
        self.app = app
        self.app_id = config["app_id"]
        self.secret_key = config["secret_key"]

    def __call__(self, environ, start_response):
        request = Request(environ)
        resp = Response(start_response)
        self._process_response(self.app_id, resp)
        apikey = request.args.get("apikey")
        if "favicon.ico" in request.path:
            return

        x = self.authorize_user(apikey=apikey, client_path=request.path)
        if x.status_code < 400:

            ##Injects duration into response time
            start_time = datetime.utcnow().timestamp()
            def injecting_start_response(status, headers, exc_info=None):
                end_time = datetime.utcnow().timestamp()
                time_taken = str(end_time - start_time)
                headers.append(('X-Time-Taken', time_taken))
                return start_response(status, headers, exc_info)
        
            return self.app(environ, injecting_start_response)
        
        res = Response(u'Authorization failed', mimetype= 'text/plain', status=x.status_code)
        return res(environ, start_response)

    @staticmethod
    def _process_response(app_id, response, request):
        #req = Request(environ, shallow=True)
        print(f'request header: {response.headers}')

【问题讨论】:

  • 修复 self._process_response(self.app_id, resp) → self._process_response(self.app_id, resp, request) 后,这确实有效。

标签: python flask


【解决方案1】:

尝试以下方法:

class SampleWSGI():    
    def __init__(self, app, config=None):
        self.app = app
        self.app_id = config["app_id"]
        self.secret_key = config["secret_key"]

    def __call__(self, environ, start_response):
        request = Request(environ)
        resp = Response(start_response)
        # This line changed:
        self._process_response(self.app_id, resp, request)
        apikey = request.args.get("apikey")
        if "favicon.ico" in request.path:
            return

        x = self.authorize_user(apikey=apikey, client_path=request.path)
        if x.status_code < 400:

            ##Injects duration into response time
            start_time = datetime.utcnow().timestamp()
            def injecting_start_response(status, headers, exc_info=None):
                end_time = datetime.utcnow().timestamp()
                time_taken = str(end_time - start_time)
                headers.append(('X-Time-Taken', time_taken))
                return start_response(status, headers, exc_info)
        
            return self.app(environ, injecting_start_response)
        
        res = Response(u'Authorization failed', mimetype= 'text/plain', status=x.status_code)
        return res(environ, start_response)

    @staticmethod
    def _process_response(app_id, response, request):
        #req = Request(environ, shallow=True)
        print(f'request header: {response.headers}')

如您所见,您没有将request 传递给_process_response()。不幸的是,简单的错误让所有人都感到遗憾......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多