Wednesday, February 17, 2016

[WSO2 ESB]Invoke Contentful API

Lets say we want to invoke Contentful API which contains the response with Content-Type:application/vnd.contentful.delivery.v2+json

1. First we tried setting the Accept header before send the message. But that didn't change the Content-Type of the  server response.
<header name="Accept" scope="transport" value="application/json"/>
2. The second attempt was to set the BinaryRelayBuilder and ExpandingMessageFormatter in the axis2.xml file under <messageBuilders> and <messageFormatters> sections.
<messageBuilder class="org.wso2.carbon.relay.BinaryRelayBuilder" contentType="application/vnd.contentful.delivery.v2+json"/>
<messageFormatter class="org.wso2.carbon.relay.ExpandingMessageFormatter" contentType="application/vnd.contentful.delivery.v2+json"/>
Now the message building error solved. But the response contains in binary format.
Eg:
<ns:binary xmlns:ns="http://ws.apache.org/commons/ns/payload">-------CAgIH0KICAgICAgICBdCiAgICAg--------</ns:binary>
3. Finally we used the default json builder and formatter with our contentful media type(application/vnd.contentful.delivery.v2+json) to resolve the issue. (make sure to remove the configuration added in above 1 and 2 steps)
Eg:
<messageBuilder class="org.apache.synapse.commons.json.JsonStreamBuilder" contentType="application/vnd.contentful.delivery.v2+json"/>
<messageFormatter class="org.apache.synapse.commons.json.JsonStreamFormatter" contentType="application/vnd.contentful.delivery.v2+json"/>