How to change HTTP Status Code in WSO2 ESB REST API - rest

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.

Related

different Content-Types in the end point response

I have a rest API with the Content-Type = application/json, provided by a tomcat server.
This means that all the responses are supposed to be in the json format.
The rest API is called by the WSO2 ESB to pass some data.
in case that the application providing the rest API is down (but the tomcat server is still up), the tomcat server replays with the http code=404 with the Content-Type=text/html (sending the HTML page "The requested resource is not available.") what results into the ESB error "Error while building message" exception and ESB crashes and losses the message.
Can you please suggest how to handle such a scenario? I'd need to receive the msg and react on this event. Is there perhaps a way how to dynamically switch content-types?
Can you try this with your own modification:
<?xml version="1.0" encoding="UTF-8"?>
<sequence xmlns="http://ws.apache.org/ns/synapse" name="fault_filter_based_http_status_code" trace="disable">
<filter regex="401" source="get-property('axis2', 'HTTP_SC')">
<then>
<makefault version="soap11">
<code xmlns:soap11Env="http://schemas.xmlsoap.org/soap/envelope/" value="soap11Env:Server"/>
<reason value="Unauthorized to access the resource"/>
<role/>
</makefault>
<send/>
</then>
<else/>
</filter>
<filter regex="500" source="get-property('axis2', 'HTTP_SC')">
<then>
<makefault version="soap11">
<code xmlns:soap11Env="http://schemas.xmlsoap.org/soap/envelope/" value="soap11Env:Server"/>
<reason value="Internal Server Error Occurred"/>
<role/>
</makefault>
<send/>
</then>
<else/>
</filter>
</sequence>
Take a look: http://harshcreationz.blogspot.com/2016/02/common-and-error-handling-sequences.html

WSO2 REST proxy answer back with SOAP instead of 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>

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"/>