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.
Using transaction markers¶
Transactions have a markers attribute that allows you to add metadata flags that can influence how the transaction
is processed by various applications. For example, you can add markers to control storage behavior:
rules:
"api":
"POST /upload":
on_request: |
transaction.markers.add("skip-request-body-storage")
See the storage markers documentation for a complete list of available markers.
Context reference¶
The following variables are available in the context of the on_request lifecycle event:
logger: the logger instance.rule: the rule name for this transaction, as defined in your configuration.transaction: theTransactioninstance.endpoint: the endpoint name for this transaction, as defined in your configuration.request: theHttpRequestinstance.response: an eventualHttpResponseinstance, but most probably None. Set this to aHttpResponseinstance 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).