on_response

The on_response lifecycle event is triggered before the response is sent back to the client, but after the proxy logic has been executed, if relevant. This logic may have been bypassed by various mechanisms, such as a forged response set by the on_request lifecycle event, or partially bypassed, for example if the proxy request did HIT the cache.

[rules."*"."*"]
on_response = """
print(f'Goodbye, {response}.')
"""
rules:
  "*":
    "*":
      on_response: |
        print(f'Goodbye, {response}.')

The event instance passed to on_response lifecycle event scripts is a ProxyFilterEvent instance.

The same event instance was passed to on_request, earlier in the lifecycle.

Patching the outgoing response

You can modify the response before it’s sent back to the client:

[rules."*"."*"]
on_response = """
response.headers['X-Goodbye'] = 'World'
"""

Switching the response

Although the reason may be debatable, if you need to, you can replace the response entirely:

[rules."*"."*"]
on_response = """
from harp.http import HttpResponse
response = HttpResponse('Goodbye, World!')
"""

The event instance passed to on_response lifecycle event scripts is a :class:`ProxyFilterEvent

Context reference

The following variables are available in the context of the on_response lifecycle event:

  • logger: the logger instance.

  • event: the ProxyFilterEvent instance.

  • endpoint: the endpoint name for this transaction, as defined in your configuration.

  • request: the HttpRequest instance.

  • response: the HttpResponse instance. You can amend or replace it.

  • stop_propagation: a function to stop the event propagation to the next event in the chain.

Warning

Don’t use stop_propagation for now, as it will stop the whole lifecycle processing (whistle#18).