WSO2 REST proxy answer back with SOAP instead of REST - rest

Recently I had a problem with wso2 esb that I cannot resolve (maybe a bug). I will try to explain it as clear as possible.
I use the wso2 ESB with a rest proxy in order to communicate from a frontend application to a backend Rest service.
I defined a rest proxy with an endpoint like this and it worked fine:
<endpoint>
<address uri="http://127.0.0.1:8099/DummyRestServiceWSO2"/>
</endpoint>
But our goal is to integrate WSO2 esb in a container inside an openshift environment. In openshift the ip are set dynamically but accessible through system environment variable. So I tried to solve my endpoint dynamically byusing the header tag:
<endpoint>
<default/>
</endpoint>
<inSequence>
<script language="js">mc.setProperty("url",java.lang.System.getenv("HOST_IP"));</script>
<property name="service_url" expression="fn:concat(get-property(url),'/DummyRestServiceWSO2')"/>
<header name="To" expression="get-property('service_ep')"/>
<send/>
</inSequence>
Where HOST_IP is defined as: http://127.0.0.1:8099
Since I made this change my call to the backend rest service is still working fine but the response to the caller (frontend) is now formatted as soap (I tried to draw the problem, see attachment).
Thanks a lot for your help and I wish a wonderfull day to whoever read this post :)

If by "REST" you mean "JSON" format, just define this property before sending back the reponse to the client :
<property name="messageType" value="application/json" scope="axis2"/>
Use application/xml for plain xml (no SOAP envelope / body)
You also can use a default endpoint in send mediator and define the format :
<send>
<endpoint>
<default format="rest">
<timeout>
<responseAction>fault</responseAction>
</timeout>
</default>
</endpoint>
</send>

Related

Rest Service on WSO2 ESB API with a Mal Formed XML request

I have created an api proxy to call my rest service, but when I send in a Mal formed XML request, I only receive an HTTP status code 202.
I have coded my service to handle this mal formed xml, and I just want the ESB to pass through the request.
Here is the code to my ESB API:
<?xml version="1.0" encoding="UTF-8"?>
<api xmlns="http://ws.apache.org/ns/synapse" name="myApi" context="/restService">
<resource methods="POST">
<inSequence>
<send>
<endpoint>
<address uri="http://myserver/MyRestService"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<send/>
</outSequence>
<faultSequence/>
</resource>
</api>
Thanks
In this case, ESB should be able to successfully send the message to the backend. By enabling wire logs you can make sure message sending correctly to the backend.
If you are not expecting any response from your backend, Please set out_only property in your inseqeunce

WSO2 HTTP Response

I am using WSO2 to send rest request, which I am able to do. But I would like to have some logic before I send the response to the client.
The Backend Rest service returns "Successful" as a response string. I have a Java code which used to do this, and I would like to use the same logic in WSO2. The logic is simple
1. If
the HTTP Status is greater than eq to 200 and less than 300 and resonseStr.equalsIgnoreCase("Successful") THEN
return "RetCode=C;Message=Success"
ELSE
return "RetCode=F;Message=Failed because Itegration Exception";
responseStr is the response from the Backend Rest Service.
My API looks like this:
<api xmlns="http://ws.apache.org/ns/synapse" name="TririgaApi" context="/tririga">
<resource methods="GET" url-mapping="/employee">
<inSequence>
<sequence key="conf:/tririgaConf"/>
<property name="POST_TO_URI" value="true" scope="axis2"/>
<send>
<endpoint>
<http trace="enable" method="GET" uri-template="http://{uri.var.service.host}:{uri.var.service.port}/html/en/default/rest/Integration?USERNAME={uri.var.service.user}&PASSWORD={uri.var.service.pass}&ioName={uri.var.wfname}"/>
</endpoint>
</send>
</inSequence>
</resource>
</api>
Thanks
Add an outSequence to your api and implement your mediation inside it
You can access to http status code with $axis2:HTTP_SC
You can test it's value with filter mediator
You can create a message with payloadFactory mediator

wso2 message soap format conversion

I use wso2 4.8.1, I have trouble with soap out messages from wso2 esb. Mesages are in soap 1.2 format 1.2 my client software expect soap messages in 1.1 format.
In my sequence file, I force soap format using the following section :
<send>
<endpoint>
<default format="soap12"/>
</endpoint>
</send>
This works fine on one of my servers, but it doesn't work on the other :
It works well on Windows XP with Java 1.6.0_13
It does not work on Windows 7 with Java 1.6.0_30
That's the only differences !
Notice that I use Axis2_pt.xml configuration file (pass through) for Axis2.
Any idea ?
Try to set messageType property :
soap1.1 : <property name="messageType" value="text/xml" scope="axis2"/>
soap1.2 : <property name="messageType" value="application/soap+xml" scope="axis2"/>
with soap1.1, you need to set the SOAP Action : use <header name="Action" value="mySoapAction"/>

How to change HTTP Status Code in WSO2 ESB REST API

I have a rest api created using WSO2 ESB.
I want to change the HTTP status code to 404 when the requested API resource does not match with the given URL.
Currently I am getting a 'HTTP/1.1 202 Accepted' response.
My esb version is 4.0.3.
Try with <property name="HTTP_SC" value="404" scope="axis2"/>
You can refer to wso2 documentation
<outSequence>
<property name="HTTP_SC" value="404" scope="axis2" />
<send/>
</outSequence>
The thing is when the URL is incorrect the request will not enter your code and will be answered by WSO2 ESB/EI itself. So in your code you don't have influence on the response in that case. Maybe it can be configured in WSO2 itself, can't find any on that yet.

WSO2 REST - ESB - REST

I'm wondering for solution to my Issue but I didn't find anything that can help me :(
THIS IS MY ISSUE:
I'd like to call a remote REST web service by passing it trought an ESB to log the client call on DB.
I'd like to pass a POST query var to my remote ws too, for example name=value & name2=value2!
I make a proxy service but I don't know how I can append the query variable to IT.
I can contact correctly remote ws with this proxy but I can't pass a POST VAR because I don't know how to do that.
I make a below curl call by client shell:
curl -k -i http://neanb330:8281/services/BioframeProxyService
in my proxy service I have this endpoint :
http://www.ebi.ac.uk/Tools/services/rest/emboss_matcher/run
BUT this service require two params in input and put out a jobid that I want to write in out sequence for client.
Have I to make a REST API?
How I can Log client call on db?
Thanks
This post describes how to process rest requests within WSO2 ESB in detail with examples.
http://wso2.com/library/articles/2012/09/get-cup-coffee-wso2-way/
This is the official documentation that explains rest url mapping
http://docs.wso2.org/display/ESB470/Getting+Started+with+REST+APIs
If your question is how you can send data to your end point using curl then this is the way
curl -v --request POST -d '<Values><name1>ABC</name1><name2>Smith</name2></Values>' -H Content-Type:"text/xml" http://neanb330:8281/services/BioframeProxyService
Then you can get the values to ESB as shown below
<property name="name1" expression="//name1/text()"/>
<property name="name2" expression="//name2/text()"/>
I find a solution for both GET and POST in Rest to rest scenario.
This for POST. I USE a proxy service and a curl call:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="BioframeProxyServiceRunBis"
transports="https,http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<log level="full"/>
<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="HTTP_METHOD" value="POST" scope="axis2" type="STRING"/>
</case>
<default/>
</switch>
<send>
<endpoint>
<address uri="http://www.ebi.ac.uk/Tools/services/rest/emboss_matcher/run/"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<send/>
</outSequence>
</target>
<description/>
</proxy>
curl -k -X POST https://neanb330:8244/services/BioframeProxyServiceRunBis -d asequence=FASTA -d bsequence=FASTA -d email=maforast#gmail.com -v
I don't find solution for save rest call to DB