Popular Posts

Saturday, July 12, 2014

How to enforce a default HTTP Content-Type for requests in WSO2 ESB

Occasionally, you will get requests from legacy client applications that do not include HTTP Content-Type header. WSO2 ESB proceeds with the mediation flow only after building the relevant SOAP infoset using the message builders registered against the Content-Type of the incoming request. In case the Content-Type is blank, you will experience an error similar to the following in the log.

ERROR - RelayUtils Error while building Passthrough stream
java.lang.StringIndexOutOfBoundsException: String index out of range: -1 

If modification of client applications to include the mandatory Content-Type HTTP header in POST requests is out of your control, we should be able to set a default content type for such incoming requests. In WSO2 ESB, you can use the following property to set a default Content-Type for the incoming requests in case the HTTP request does not have Content-Type header.

i.e:-


<parameter name="DEFAULT_REQUEST_CONTENT_TYPE" locked="false">application/json</parameter>

Set this property in ESB_HOME/repository/conf/axis2/axis2.xml and restart the server. Here, we have configured application/json as the default content type. So that, when a request similar to the following reaches ESB, the default JSON builder registered for application/json content (org.apache.synapse.commons.json.JsonStreamBuilder in WSO2 ESB-4.8.1) takes care of building the message and hands it over to mediation engine to proceed with the mediation flow.


POST http://host:8280/services/ContentTest HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Length: 30
Host: host:8280
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

{
  "in": { "test": "wso2" }
}

1 comment:

Susinda Perera said...

Thanks for sharing Charitha ayya, this solves a common problem that anyone could face. Helped me a lot.