how to read http headers in esb

Ron Peterson picture Ron Peterson · Sep 15, 2014 · Viewed 10.3k times · Source

How do I set a property to the value of an incoming http request header? I tried a few things (see following), but my log values are all null, so I'm clearly not reading the header values correctly. The header value I really care about is X-EMPID. Using wso2esb 4.8.1.

Here are a couple of posts that led me to believe this would work, but I'm not having any luck yet.

<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="getaccount2"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <property name="empid"
                   expression="get-property('transport', 'X-EMPID')"
                   scope="default"
                   type="STRING"/>
         <log level="custom">
            <property name="emp_id" expression="get-property('empid')"/>
         </log>
         <log level="custom">
            <property name="content_length"
                      expression="get-property('transport', 'Content-Length')"/>
         </log>
         <log level="custom">
            <property name="TRANSPORT_HEADERS" expression="get-property('TRANSPORT_HEADERS')"/>
         </log>

Answer

Kallja picture Kallja · Sep 17, 2014

You can conveniently access HTTP headers, which technically are transport headers in WSO2 ESB, by using XPath variables. The easiest way to read an HTTP header named X-EMPID is by using the following XPath: $trp:X-EMPID, where the $trp prefix indicates that the part following the colon is the name of a transport property. To log the header value you could use the following log mediator:

<log level="custom">
    <property name="X-EMPID value" expression="$trp:X-EMPID" />
</log>

To set the property myProperty to the value of X-EMPID HTTP header (which is already stored in a transport property) you would use the property mediator:

<property name="myProperty" expression="$trp:X-EMPID" />

The functionality is documented on the WSO2 site.