StackCheats

Filter Request Headers

Using Filter Mediator to Filter Request Headers

November 15, 2019

wso2mediatorsynapse-sequence

Stratagem

A Quick-Peak on using Filter mediators to filter and execute custom executions based on presented request header.

For example, check for the mycustomheader header value in the request and print the value using Log mediator.

<sequence name="custom-sequence"
xmlns="http://ws.apache.org/ns/synapse">
source="get-property('mycustomheader', 'transport')">
<filter>
<then></then>
<else></else>
</filter>
</sequence>
source="get-property('mycustomheader', 'transport')">
</sequence>
source="get-property('mycustomheader', 'transport')">
</sequence>
source="get-property('mycustomheader', 'transport')">
</sequence>

Given is a simple Synapse sequence with a Filter mediator skeleton.

We can use the get-property() method to access and retrieve properties from the Synapse context. We will be using get-property('mycustomheader', 'transport') to retrieve our mycustomheader value from the Synapse context using the scope as transport.

Next we will add some Log mediators in both then and else sections of the Filter mediator to print and log success and failure messages.

Now we have successfully written and modified our code to capture and retrieve the someHeaders value from the request headers. We will now print some custom message on success as well as on failure methods to prompt in the carbon logs

Given below is the complete sample Synapse sequence (in-sequence) used to filter request Headers using Filter mediator

<sequence name="custom-sequence" xmlns="http://ws.apache.org/ns/synapse">
<filter source="get-property('mycustomheader', 'transport')">
<then>
<log level="custom">
<property name="message" value="Header is presented" />
</log>
</then>
<else>
<log level="custom">
<property name="message" value="Header is not presented" />
</log>
</else>
</filter>
</sequence>

Athiththan Kathirgamasegaran