Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
Field does not add into header in quart / flask middleware

I’m trying to log data in Flask Middleware. I use a process_response function to log the response data.

I have this code block where I’m trying to inject the time duration to the headers of the response. However the X-Time-Taken isn't being added to the header. The response header is request header: Content-Type: text/plain; charset=utf-8.

Why is X-Time-Taken not being added to the header?

FYI, I'm basing my code off this which suggests that what I'm doing should work.

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}')
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Try the following:

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}')

As you see, you did not pass request to _process_response(). Simple mistakes get aus all unfortunately...

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda