Sudha Ch
Sudha Ch

Reputation: 147

How to remove Mule default headers and set Headers received from Third party API

Please help in setting HTTP Headers in mule. I want to remove default mule headers and send the HTTP headers received from third party API as response headers

I tried by using following groovy code. But it didnt work.

import org.json.JSONObject;
import java.lang.String;
import java.util.*;
import java.util.Map;
import java.util.Map.Entry;

JSONObject cResponse = new JSONObject(payload);
System.out.println("http.headers:"+message.getInboundProperty('http.headers'));
Map<String, Object> headers = (Map<String, Object>) message.getInboundProperty('http.headers');
System.out.println("111"+headers);
payload.removeHeader(payload.removeHeaders("Server"));

for (Entry<String, Object> entry : headers.entrySet()) {

                        String key = entry.getKey();
                        Object value = entry.getValue();
                        System.out.println("Key & Values are: "+key +" & "+ value);
                        message.setInvocationProperty(key, value);

}
payload=cResponse;

Thanks in advance!

Upvotes: 2

Views: 2733

Answers (1)

Mauro Rocco
Mauro Rocco

Reputation: 5128

You don't need to use groovy or any java code you can just use the remove properties and copy properties component. For example this snippet will copy all the inbound http to outbound and remove properties prefixed wiht some mule common strings.

<remove-property propertyName="MULE_*"/>
<remove-property propertyName="X_MULE*"/>
<remove-property propertyName="x-mule*"/>
<copy-properties propertyName="http.*"/>

However I would suggest you keep the removal of Mule like properties but for populating outbound properties from inbound I would do a selective copy to avoid unwanted behavior. You could do something like this.

<remove-property propertyName="MULE_*"/>
<remove-property propertyName="X_MULE*"/>
<remove-property propertyName="x-mule*"/>
<copy-property propertyName="Content-Length"/>
<copy-property propertyName="Transfer-Encoding"/>
<copy-property propertyName="Keep-Alive"/>
<copy-property propertyName="Connection"/>

Hope this helps

Regards

Upvotes: 3

Related Questions