I know that I can read fault related variables within an error flow, but how can I access request-related context variables in the fault flow. For example, within a failed authentication fault, I would like to access and log the username from the request. This way I would know which account attempted to login. Is this possible?
Hi @Michael Atkins Accessing request and response variables in Fault flow in exactly same as you would do in a proxy or a target flow. If you want to log the username, which is present in the request header you use a MessageLogging policy in Fault Rules section. You can access username as {request.header.username} in the policy.
I have attached a simple node.js hello world proxy with this answer. In this proxy I am verifying the API key. If the API key is invalid, I am returning a custom error message with the username field. I am assuming Username is passed as a header in the request. In your case, you will have to use a Message logging instead of RaiseFuault.
Proxy Configuration -
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ProxyEndpoint name="default">
<Description/>
<PreFlow name="PreFlow">
<Request>
<Step>
<Name>Verify-API-Key-1</Name>
</Step>
</Request>
<Response/>
</PreFlow>
<Flows/>
<PostFlow name="PostFlow">
<Request/>
<Response/>
</PostFlow>
<HTTPProxyConnection>
<BasePath>/hello-world-nodejs-faultrule</BasePath>
<VirtualHost>default</VirtualHost>
<VirtualHost>secure</VirtualHost>
</HTTPProxyConnection>
<RouteRule name="default">
<TargetEndpoint>default</TargetEndpoint>
</RouteRule>
<FaultRules>
<FaultRule name="invalid_key_rule">
<Step>
<Name>fault_invalidkey</Name>
</Step>
<Condition>(fault.name = "InvalidApiKey")</Condition>
</FaultRule>
</FaultRules>
</ProxyEndpoint>
RaiseFault policy -
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RaiseFault async="false" continueOnError="false" enabled="true" name="fault_invalidkey">
<DisplayName>fault_invalidkey</DisplayName>
<Properties/>
<FaultResponse>
<Set>
<Headers/>
<Payload contentType="text/plain">
{request.header.username} is invalid. Please pass a valid Key
</Payload>
<StatusCode>401</StatusCode>
<ReasonPhrase>Auth Error</ReasonPhrase>
</Set>
</FaultResponse>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</RaiseFault>
Hope this helps!
As Sudhendra says you have access to context variables in policies through a simple reference mechanism. If you are using the out of the box Messsage Logging policy you can do the following:
<MessageLogging name="LogToSyslog">>
<Syslog>
<Message>[3f509b58 tag="{organization.name}.{apiproxy.name}.{environment.name}"] Weather request for WOEID {request.queryparam.w}.</Message>
<Host>logs-01.loggly.com</Host>
<Port>514</Port>
<Protocol>TCP</Protocol>
</Syslog>
</MessageLogging>
Anything in {} is a reference to a context variable. More information at: http://apigee.com/docs/api-services/reference/message-logging-policy