Unable to get the response from the API - wso2

Scenario
I'm sending a request payload to the API that further calls the SMS service provider in the in-sequence flow, I need to share back the response from the SMS service provider as it is. The API works fine and I do receive SMS on phone but I'm unable to share back the response from the service provider in the out sequence flow.
The response body from my SMS service provider is actually text as shown:
The Response header of SMS Service Provider looks like this:
API
<?xml version="1.0" encoding="UTF-8"?>
<api context="/mobilink" name="MobilinkSmsApi" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST" uri-template="/send">
<inSequence>
<property action="remove" name="TRANSPORT_HEADERS" scope="axis2"/>
<property description="username" expression="json-eval($.username)" name="uri.var.username" scope="default" type="STRING"/>
<property description="password" expression="json-eval($.password)" name="uri.var.password" scope="default" type="STRING"/>
<property description="to" expression="json-eval($.to)" name="uri.var.to" scope="default" type="STRING"/>
<property description="from" expression="json-eval($.from)" name="uri.var.from" scope="default" type="STRING"/>
<property description="message" expression="json-eval($.message)" name="uri.var.message" scope="default" type="STRING"/>
<log level="full"/>
<call>
<endpoint>
<http method="post" statistics="enable" trace="enable" uri-template="https://coXXXXX.XXXX.com/sendsms_url.html?Username={uri.var.username}&Password={uri.var.password}&From={uri.var.from}&To={uri.var.to}&Message={uri.var.message}">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>-1</progressionFactor>
<maximumDuration>0</maximumDuration>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</http>
</endpoint>
</call>
</inSequence>
<outSequence>
<log category="TRACE" level="full"/>
<property description="Content-Type" name="Content-Type" scope="default" type="STRING" value="text/html"/>
<property name="messageType" scope="axis2" type="STRING" value="text/html"/>
<respond/>
</outSequence>
<faultSequence>
<log category="ERROR" level="full"/>
</faultSequence>
</resource>
</api>
Question
My Question is how to share back the response from the service provider in the out sequence? I tried to use property with value text/html and even used content-type as text/html but It didn't worked.

There are two ways to achieve this. either one of below mentioned way you can follow
place <respond/> after <call> mediator
Use <Send> mediator instead of <call>, so that flow will come from insequence to outsequence where you already placed <respond/> to send back message to client

The issue got resolved when I placed the mediator in the inSequence flow.
previously I was placing the mediator in the outSequence

Related

WSo2 API Manager In Sequence to use Call Mediator to call a rest API which return empty JSON payload response but values in response headers

we are using WSo2 API Manager 2.1.0.
when configuring an API in API Publisher we wanted to add a sequence of calls before calling the target API. So we added the In Sequence flow, where in a call Mediator we calling a REST API which return the values in response headers and a empty JSON Payload as 200.
In WSO2 API Manager we are getting the below response.
<am:fault xmlns:am="http://wso2.org/apimanager">
<am:code>401001</am:code>
<am:type>Status report</am:type>
<am:message>Runtime Error</am:message>
<am:description>Payload is not a JSON string.</am:description> </am:fault>
<header name="Content-Type" scope="transport" value="application/json"/>
<property name="messageType" scope="transport" type="STRING" value="application/json"/>
<log level="full"/>
<header name="site_id" scope="transport" value="004ada8a-6a11-f080-f56c-12dd3808e8f7"/>
<header expression="fn:concat('Bearer ', get-property('generated-access-token'))" name="Authorization" scope="transport" xmlns:ns3="http://org.apache.synapse/xsd" xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"/>
<call blocking="true">
<endpoint>
<http method="put" uri-template="https://wso2.domainname.com/api/v1.0/users/me/login-defaults">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>1</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</http>
</endpoint>
</call>
<log level="full"/>
<property description="session-id" expression="$trp:X-NG-SessionId" name="session-id" scope="default" type="STRING"/>
<log level="custom">
<property expression="$trp:X-NG-SessionId" name="Access Token after call if block"/>
</log>
<property expression="$ctx:resource" name="REST_URL_POSTFIX" scope="axis2" type="STRING"/>

In wso2 esb how to call websokcet published by api manager

I have published a websocket api as given in the Documentation, So I wanted to call that websocket API through wso2 ESB. So, how to pass the Auth header as I'm getting the following error on the console.
[2018-10-02 18:24:36,503] ERROR - InboundWebsocketSourceHandler Endpoint not found for port : 9099 tenant domain : null
Below is the ESB to call websocket API:
<api context="/test" name="test" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST">
<inSequence>
<property name="OUT_ONLY" scope="default" type="STRING" value="true"/>
<log level="full">
<property name="message" value="********************************************input**********************"/>
</log>
<property name="Authorization" scope="transport" type="STRING" value="Bearer cf920eb2-960c-383c-b556-78e9258fd8d3"/>
<send>
<endpoint>
<http method="post" uri-template="ws://localhost:9099/echowebsocket/1.0"/>
</endpoint>
</send>
</inSequence>
<outSequence>
</outSequence>
<faultSequence/>
</resource>
</api>
outflow sequence:
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="outflowDispatchSeq" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<log level="full">
<property name="message" value="************************************************"/>
</log>
</sequence>

Adding parameters to the endpoint url wso2 esb

I am new to the WSO2 esb. I want to create a api in which i want to add a dynamic parameter from api Url to endpoint url.
My end point url is like http://example.com/api/sync/{session_id}.json
I tried to create api like
<api name="rest" context="/sync">
<resource methods="GET" uri-template="/{id}">
<inSequence>
<log level="full">
<property xmlns:ns="http://org.apache.synapse/xsd"
name="uri.var.session"
expression="get-property('uri.var.id')"
scope="axis2"/>
</log>
<property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>
<send>
<endpoint name="Mecars">
<http trace="enable"
method="get"
uri-template="http://example.com/sync/{+uri.var.session}.json"/>
</endpoint>
</send>
</inSequence>
</resource>
</api>
In log i get the value of uri.var.session But on the endpoint Uri-template it is not appending.
Please guid me how append the {id} value in the api uri-template to end point uri-template?
Check this sample
I recommend you to do string concatenation at property mediator adn use that directly at uri-template, rather adding "variable +.json" at uri-template.
That is;
<property xmlns:ns="http://org.apache.synapse/xsd"
name="uri.var.session"
expression="get-property('uri.var.id')"
scope="axis2"/>
In the above for expression do string concatenation to construct full variable with ".json" ending.
Please remove the '+' symbol from your http end point.Its working fine to me
sample API
<api xmlns="http://ws.apache.org/ns/synapse" name="Elastic Search Using NPI" context="/api/npi/professional/npi.json">
<resource methods="OPTIONS GET" uri-template="/{npi}">
<inSequence>
<log level="full">
<property name="uri.var.npi" expression="get-property('uri.var.npi')"></property>
</log>
<send>
<endpoint>
<http method="get" uri-template="example.com/{uri.var.npi}"></http>
</endpoint>
</send>
</inSequence>
</resource>
</api>
Change the http endpoint uri template like follows.
<http trace="enable" method="get" uri-template="http://example.com/sync/{uri.var.id}.json"></http>
Following is the modified api configuration.
<api xmlns="http://ws.apache.org/ns/synapse" name="rest" context="/sync">
<resource methods="GET" uri-template="/{id}">
<inSequence>
<log level="full">
<property xmlns:ns="http://org.apache.synapse/xsd" name="uri.var.session" expression="get-property('uri.var.id')"></property>
</log>
<property name="REST_URL_POSTFIX" scope="axis2" action="remove"></property>
<send>
<endpoint name="Mecars">
<http trace="enable" method="get" uri-template="http://example.com/sync/{uri.var.id}.json"></http>
</endpoint>
</send>
</inSequence>
</resource>
</api>
You can do the string concatenation at the property mediator as follows.
<property xmlns:ns="http://org.apache.synapse/xsd"
name="uri.var.session"
expression="fn:concat(get-property('uri.var.id'),'.json')"
scope="axis2"/>

Can we Set a Send mediator with Xpath expression

i am trying to send a message to my email for that i am using send mediator as well i setup required configurations in AXIS2 file
is it work for below proxy
if not what is the way to give xpath to send mediator
<proxy xmlns="http://ws.apache.org/ns/synapse" name="mailCheck" transports="http" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence>
<property name="Subject" value="Alert Message From WSO2 ESB - Service Down !!!" scope="transport" type="STRING"/>
<property name="messageType" value="text/html" scope="axis2" type="STRING"/>
<property name="ContentType" value="text/html" scope="axis2" type="STRING"/>
<property name="Mail" value="mailto:faisal.shaik#youtility.in" scope="default" type="STRING"/>
<log level="full">
<property name="Mail" value="mailto:faisal.shaik#youtility.in"/>
</log>
<property name="OUT_ONLY" value="true" scope="default" type="STRING"/>
<send>
<endpoint key-expression="get-property('Mail')"/>
</send>
</inSequence>
<outSequence/>
</target>
<description></description>
</proxy>
if need any changes pls let me know
if you want to get the email address from a property value then use the header mediator to set value of "To" to "mailto:faisal.shaik#youtility.in".
You can do that adding the following before the send mediator
<header name="To" expression="fn:concat('mailto:', get-property('Mail'))"/>
You can use this:
<property name="To" expression="get-property('uri.var.to')" scope="transport"/>
<send>
<endpoint>
<address uri="mailto:"/>
</endpoint>
</send>
<send>
<address uri="mailto:xxx#yyy"/>
</send>
key-expression also can be used..

WSO2 ESB - transform a HTTP 202 accepted response

The Endpoint_BPS_CreateCaseService/UpdateCaseService endpoints below both point to one-way BPEL services running on WSO2 BPS. WSO2 BPS returns a HTTP 202 accepted message instantly when they are invoked.
The client application that I am using will throw a fault if it does not get a valid SOAP envelope as a response so I'm going to use a proxy service in ESB to wrap around the BPEL process.
How do I use a WSO2 ESB proxy service to forward a SOAP envelope to Endpoint_BPS_* below and then return a SOAP envelope response to my client app?
I also want to execute the faultSequence "ProcessFault" if either endpoint is unavailable or times out. I previously used the OUT_ONLY to get around the response issue above but it means I can't detect endpoint problems. Unless it is possible to do both somehow?
Another thing I've tried is cloning the message but this was a bit messy.
Any help greatly appreciated
<proxy xmlns="http://ws.apache.org/ns/synapse" name="BPSProxyService" transports="https,http" statistics="disable" trace="enable" startOnLoad="true">
<target faultSequence="ProcessFault">
<inSequence>
<log level="full">
<property name="MESSAGE" value="BEGIN BPSProxyService" />
</log>
<switch source="//*[local-name()='Operation']">
<case regex="create">
<send>
<endpoint key="Endpoint_BPS_CreateCaseService" />
</send>
</case>
<case regex="update">
<send>
<endpoint key="Endpoint_BPS_UpdateCaseService" />
</send>
</case>
</switch>
</inSequence>
<outSequence>
<property name="HTTP_SC" value="200" scope="axis2" />
<class name="esb.mediators.InjectSOAPEnvelope" />
<log level="full">
<property name="MESSAGE" value="END BPSProxyService" />
</log>
<send />
<drop />
</outSequence>
</target>
<publishWSDL key="common/bpsproxyservice/bpsproxyservice.wsdl">
<resource location="schema.xsd" key="common/schema_v2.xsd" />
</publishWSDL>
</proxy>
When you receive a 'HTTP/1.1 202 Accepted' response from your backend like BPS in the outSequence of your Proxy Service, then you need the <property name="SC_ACCEPTED" value="false" scope="axis2"/> statement to modify the '202'-response into something else.
Example:
<property name="SC_ACCEPTED" value="false" scope="axis2"/>
<property name="HTTP_SC" value="200" scope="axis2"/>
<payloadFactory media-type="xml">
<format>
<response>
<result>OK</result>
</response>
</format>
<args/>
</payloadFactory>
<send/>
The response is transformed into 'HTTP/1.1 200 OK' with a response message.
Add the "FORCE_SC_ACCEPTED" parameter with the "OUT_ONLY" in the inSequence of the proxy service as follows.
<property name="FORCE_SC_ACCEPTED" value="true" scope="axis2" type="STRING"/>
<property name="OUT_ONLY" value="true" scope="default" type="STRING"/>
For more information use the following article:
http://mohanadarshan.wordpress.com/2013/05/05/out_only-scenario-in-proxy-service-wso2-esb/