user2720472
user2720472

Reputation: 61

how to get the response of the rest api and pass that response to another service in wso2 esb?

<resource methods="GET" uri-template="/getTypeCodes" faultSequence="service_error_handler_">
  <inSequence>
     <log level="custom">
        <property name="CommonService" value="*************getTypeCodes called**************"/>
        <property name="Request Payload" expression="get-property('JSON_OBJECT')"/>
     </log>
     <property name="HTTP_METHOD" value="GET" scope="axis2" type="STRING"/>
     <property name="messageType" value="application/json" scope="axis2" type="STRING"/>
     <sequence key="oauthMediationService"/>
     <property name="uri.var.servicename" value="commonservice"/>
     <send>
          <endpoint>
           <address uri="http://localhost:8080/rest/commonservice/getTypeCodes" format="rest"/>
        </endpoint>
     </send>
     <log level="custom">
        <property name="getTypeCodeResponse" expression="$body"/>
     </log>
  </inSequence>
  <outSequence>
     <send/>
  </outSequence>

From the above rest example configuration i am calling the service in endpoint. After calling the endpoint i need to get the response and send that response to the the another endpoint based on condition.

Upvotes: 1

Views: 2032

Answers (2)

Dharshana
Dharshana

Reputation: 1232

You can use following configuration to call to reset service and get a response. In below sample I'm using HTTP endpoint

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="peoplePutProxy"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
    <target>
        <inSequence>
            <property name="HTTP_METHOD" value="GET" scope="axis2"/>
            <property name="messageType"
                      value="application/x-www-form-urlencoded"
                      scope="axis2"/>
            <send>
                <endpoint>
                    <http method="post"
                          uri-template="http://localhost:8080/rest/api/people?email={uri.var.email}&firstName={uri.var.fname}&lastName={uri.var.lname}"/>
                    <property name="uri.var.fname" value="dhar"/>
                    <property name="uri.var.email" value="[email protected]"/>
                    <property name="uri.var.lname" value="kasun"/>
                </endpoint>
            </send>
        </inSequence>
        <outSequence>
            <log level="full"/>
            <property name="messageType" value="text/xml" scope="axis2"/>
            <send/>
        </outSequence>
    </target>
    <description/>
</proxy> 

Http end point is where users can specify an URI Template which can dynamically populate final URI for the RESTful service invocation. Also, users can manipulate HTTP method of the outgoing request. Please refer [1] for more information on http endpoint

[1]. http://docs.wso2.org/display/ESB470/HTTP+Endpoint

Upvotes: 2

Nufail
Nufail

Reputation: 1598

Your requirement is called 'Service Chaining'. This blog post explains how to achieve service chaining in WSO2 ESB. Go through the other article linked at the start of that blog to get a better understanding. They provide a complete example of service chaining.

Basically you can specify a sequence as the receiver of the response in a send mediator as follows.

 <send receive="RespSequence">
      <endpoint>
       <address uri="http://localhost:8080/rest/commonservice/getTypeCodes" format="rest"/>
    </endpoint>
 </send>

In this case the response from calling the endpoint will be directed to RespSequence. So in that sequence you can specify the other endpoint.Refer Send Mediator doc for more info. Use Switch Mediator to check for conditions.

Upvotes: -1

Related Questions