on_request
¶
The on_request
lifecycle event happens on each request received by the proxy, before it even considers proxying
anything anywhere.
[rules."*"."*"]
on_request = """
print(f'Hello, {request}.')
"""
rules:
"*":
"*":
on_request: |
print(f'Hello, {request}.')
The event instance passed to on_request lifecycle event scripts is a
ProxyFilterEvent
instance.
The same event instance will be passed to on_response, later in the lifecycle.
Patching the incoming request¶
You get a chance to modify the request before the proxy logic kicks in:
[rules."*"."*"]
on_request = """
request.headers['X-Hello'] = 'World'
"""
Forging a response¶
If a Response
is set in this event, the proxy will not be involved at all.
[rules."*"."*"]
on_request = """
from harp.http import HttpResponse
response = HttpResponse('Hello, World!')
"""
It’s defined and dispatched by the proxy
application, within the
HttpProxyController
request handler.
Context reference¶
The following variables are available in the context of the on_request
lifecycle event:
logger
: the logger instance.event
: theProxyFilterEvent
instance.endpoint
: the endpoint name for this transaction, as defined in your configuration.request
: theHttpRequest
instance.response
: an eventualHttpResponse
instance, but most probably None. Set this to aHttpResponse
instance to short-circuit the proxying logic.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).