Search code examples
wso2esb

Adding http path parameters dynamically in wso2 esb


We have a proxy service which uses jms transport to receive messages. The messages received need to be sent to a backend REST service using http POST. The following is done on the messages

  1. xslt transformation to extract specific fields
  2. set message type to application/json
  3. send to the endpoint

The REST service endpoint needs to have a path parameter appended dynamically using one of the values that comes as part of the input message from jms. The url will look like http://<server-ip>/service/<client>. Here the value for the "client" comes as part of the message.

How can we dynamically add the path param using wso2 esb?


Solution

  • I believe what you are looking for is the REST_URL_POSTFIX property. If you set this property, the value will be appended to the rest endpoint url.

    It can be defined as follows with the scope of axis2.

    <property name="REST_URL_POSTFIX"
              expression="//client"
              scope="axis2"
              type="STRING"/>
    

    An example on this can be found in this guide, Using REST with a Proxy Service.

    EDIT: Following is example using a simple proxy with a POST request using curl. Providing as per the comments. Here, I'm invoking the jaxrs_basic rest service in WSO2 Application Server.

    curl -H "Content-Type: application/xml" -H "Accept: application/json" -d "<Customer><name>KasunG</name></Customer>" http://localhost:8281/services/new1/
    

    .

    curl -H "Content-Type: application/json" -H "Accept: application/json" -d "{ 'Customer' : { 'name' : 'KasunG' } }  " http://localhost:8281/services/new1/
    

    .

    <?xml version="1.0" encoding="UTF-8"?>
    <proxy xmlns="http://ws.apache.org/ns/synapse"
           name="new1"
           transports="https http"
           startOnLoad="true"
           trace="disable">
       <description/>
       <target>
          <inSequence>
             <property name="REST_URL_POSTFIX"
                       value="customers"
                       scope="axis2"
                       type="STRING"/>
             <property name="ContentType" value="text/xml" scope="axis2" type="STRING"/>
             <switch source="$axis2:HTTP_METHOD">
                <case regex="GET">
                   <property name="HTTP_METHOD" value="GET" scope="axis2" type="STRING"/>
                </case>
                <case regex="POST">
                   <property name="messageType" value="application/json" scope="axis2"/>
                   <property name="ContentType"
                             value="application/JSON"
                             scope="axis2"
                             type="STRING"/>
                   <property name="HTTP_METHOD" value="POST" scope="axis2" type="STRING"/>
                </case>
                <default/>
             </switch>
             <send>
                <endpoint>
                   <address uri="http://localhost:8888/jaxrs_basic/services/customers/customerservice"
                            format="rest"/>
                </endpoint>
             </send>
          </inSequence>
          <outSequence>
             <property name="messageType" value="application/json" scope="axis2"/>
             <send/>
          </outSequence>
       </target>
    </proxy>