How to use Apache Synapse callout mediator to send a given transport header

214 Views Asked by At

I'm trying to leverage the callout mediator to call a REST endpoint.

But I need to send the contents of a received HTTP header.

I tried:

    <callout passHeaders="false">
        <endpoint>
            <address uri="http://localhost:8888/test"></address>
        </endpoint>
        
        <source xpath="$trp:X-custom-header" />
    </callout>

But this failed with:

 ERROR - CalloutMediator The evaluation of the XPath expression : $trp:X-custom-header did not result in an OMElement

Is it even possible?

1

There are 1 best solutions below

2
Novren On

If your header is already available via the $trp scope it should be sent to your endpoint just fine.

If it's not set yet your can set it via the header mediator or the property mediator like so:

<property name="transportHeaderName" value="something" scope="transport"/>

If you need form data like mentioned below you will need to use the payload mediator to create a payload and set the messagetype to multipart/form-data (You might need to enable an extra formatter in the axis2.xml for this)

Payload:

     <payloadFactory media-type="xml">
        <format>
           <root>
              <x-custom-field>text</x-custom-field>
           </root>
        </format>
        <args>
           <arg evaluator="xml" expression="$trp:x-custom-header"/>
        </args>
     </payloadFactory>


<property name="messageType" value="multipart/form-data" scope="axis2"/>