WSO2 ESB - REST API - Response Does Not Come - wso2-esb

I have exposed a REST service using <api> in wso2 esb. But the response does not come. REST service is written in Apache Wink.
API
<api name="API_2" context="/hello" hostname="localhost" port="8080">
<resource url-mapping="/name" methods="GET">
<inSequence>
<log level="full"/>
<property name="messageType" value="text/plain" scope="transport" type="STRING"/>
<send>
<endpoint>
<address uri="http://localhost:8080/HelloService/rest/test/hello"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<log level="full"/>
<send/>
</outSequence>
</resource>
</api>
esb log
[2013-12-11 12:28:24,643] INFO - API Initializing API: API_2
[2013-12-11 12:28:35,467] INFO - LogMediator To: /hello/name, MessageID: urn:uuid:52d2ddf1-301e-42e0-ac9d-ac4a57ac8c72, Direction: request

I think your endpoint address is wrong , look like you have repeat hello twice. since you have hello in proxy name also which will append to URL.
try <address uri="http://localhost:8080/HelloService/rest/test"/>
Also You can verify your backend works by calling "uri + url-mapping" in separate browser
I have shown sample API works for me below, you should have called your API as below and log will print as below.
URL to call: http://localhost:8280/TestAPI/customerservice/customers/123
INFO - LogMediator To: /TestAPI/customerservice/customers/123
<api xmlns="http://ws.apache.org/ns/synapse" name="TestAPI" context="/TestAPI">
<resource methods="GET" url-mapping="/customerservice/customers/123">
<inSequence>
<log/>
<send>
<endpoint>
<address uri="http://localhost:9764/jaxrs_basic/services/customers"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<log/>
<send/>
</outSequence>
</resource>
</api>

Related

wso2 ei 6.5 SOAP Client and REST Service The parameter received is XML Rest need to json

<proxy xmlns="http://ws.apache.org/ns/synapse"
name="ctc"
startOnLoad="true"
statistics="enable"
trace="disable"
transports="http,https">
<target>
<inSequence>
<log>
<property name="******" value="---ctc---"/>
</log>
<property xmlns:saop="http://j2ee.netbeans.org/wsdl/EsbJyBpel/src/saopWSDL"
expression="$body//saop:upload/xml"
name="request_msg"/>
<log>
<property expression="json-eval($)" name="request_msg"/>
</log>
<property name="messageType"
scope="axis2"
type="STRING"
value="application/json"/>
<script language="js">mc.setPayloadJSON(
{
"Code" : "10",
"Price" : "11"
});</script>
<property name="HTTP_METHOD" scope="axis2" value="POST"/>
<property name="messageType"
scope="axis2"
type="STRING"
value="application/json"/>
<property name="Content-Type"
value="application/json"
scope="transport"
type="STRING"/>
<send>
<endpoint>
<address format="pox" uri="http://192.16.110.142:8081/dqcsweb/ES/test"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<log>
<property expression="json-eval($)" name="res_msg"/>
</log>
<send/>
</outSequence>
</target>
</proxy>
The XML structure that the restful service log prints;
How do You convert an XML structure to JSON?
I used script and payloadFactory, output the JSON string in the ESB, and the print log in the invoked service is still XML
input:
enter image description here
The message received by the service(http://192.16.110.142:8081/dqcsweb/ES/test):
<jsonObject><Code>10</Code><Price>11</Price></jsonObject>
expectations:
{
"code":"10",
"Price":"11"
}
Can you change the format of the endpoint invoked to rest. Please refer to the follwoing sample configuration.
<send>
<endpoint>
<address format="rest" uri="http://192.16.110.142:8081/dqcsweb/ES/test"/>
</endpoint>
</send>
The used pox format results in the message transformed into plain old XML format. Please refer to the documentation [1] to further clarify this.
[1]-https://docs.wso2.com/display/EI611/Address+Endpoint
Update
Can you enable wire logs by uncommenting the following [2] in log4j.properties file. You need to perform a server restart to enable this. This allows you to see the request sent out of the EI server. Please check if the request going out of the EI contains the necessary payload.
log4j.logger.org.apache.synapse.transport.http.wire=DEBUG
[2]-https://docs.wso2.com/display/ESB500/Debugging+Mediation#DebuggingMediation-Viewingwirelogs

wso2esb remove context path from uri

I'm defining an API in wso2esb for one of the tenants. This tenant automatically gets it's own context path when defining an API (e.g. http://127.0.0.1:8280/tenant1/api-identifier/).
The next step is that this request gets forwarded to an http endpoint (e.g. http://127.0.0.1:9191/v1/). But when looking at the logs the esb tries to send this request to http://127.0.0.1:9191/v1/tenant1/api-identifier/ which can't give a response. Can I remove this part from the url. It feels like this should work out of the box.
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://ws.apache.org/ns/synapse">
<registry provider="org.wso2.carbon.mediation.registry.WSO2Registry">
<parameter name="cachableDuration">1500</parameter>
</registry>
<taskManager provider="org.wso2.carbon.mediation.ntask.NTaskTaskManager"/>
<sequence name="In">
<log category="TRACE" level="full"/>
<send>
<endpoint key="gov:/HelloWorld"/>
</send>
</sequence>
<sequence name="fault">
<log level="full"/>
</sequence>
<sequence name="main">
<in>
<log/>
<sequence key="conf:/InSecure"/>
</in>
<out>
<send/>
</out>
</sequence>
<sequence name="Out">
<send/>
</sequence>
<sequence name="InSecure">
<log/>
<send>
<endpoint key="gov:/HelloWorld"/>
</send>
</sequence>
<api context="/lensworld.be/hw" hostname="192.168.1.100"
name="HelloWorld" port="9191">
<resource inSequence="InSecure" methods="GET" outSequence="Out"/>
</api>
Try removing the REST_URL_POSTFIX property:
<property name="REST_URL_POSTFIX" action="remove" scope="axis2"/>
From the docs:
The value of this property will be appended to the target URL when sending messages out in a RESTful manner through an address endpoint. This is useful when you need to append a context to the target URL in case of RESTful invocations. If you are using an HTTP endpoint instead of an address endpoint, specify variables in the format of "uri.var.*" instead of using this property.

WSO2ESB REST API receives Microsoft Excel Worksheet (.xlsx) from attachment and Generate doc type error

I have created an REST API that accepts a Microsoft Excel Worksheet (.xlsx) file as POST input. Code is mentioned below:
<api xmlns="http://ws.apache.org/ns/synapse" name="classroomcourse" context="/classroomcourse">
<resource methods="POST">
<inSequence>
<log level="full"/>
<property name="messageType" value="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" scope="axis2"/>
<send>
<endpoint>
<http uri-template="http://qa-aaa.ggg.com/xyz/api/importCourses"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<log level="full"/>
<property name="messageType" value="application/json" scope="axis2"/>
<send/>
</outSequence>
When I send request via POSTMAN and attach xlsx file using form-data then I get the below error on the ESB Console:
[2016-03-02 17:32:13,332] ERROR - Error while building Passthrough stream {org.apache.synapse.transport.passthru.util.RelayUtils} org.apache.axiom.om.OMException: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '"' (code 34) in DOCTYPE declaration; expected a space be tween public and system identifiers at [row,col {unknown-source}]: [1,50] at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:296) at org.apache.axiom.om.impl.llom.OMDocumentImpl.getOMDocumentElement(OMDocumentImpl.java:109) at org.apache.axiom.om.impl.builder.StAXOMBuilder.getDocumentElement(StAXOMBuilder.java:570) at org.apache.axiom.om.impl.builder.StAXOMBuilder.getDocumentElement(StAXOMBuilder.java:566) at org.apache.synapse.transport.passthru.util.DeferredMessageBuilder.getDocument(DeferredMessageBuilder.java:149) at org.apache.synapse.transport.passthru.util.RelayUtils.builldMessage(RelayUtils.java:133) at org.apache.synapse.transport.passthru.util.RelayUtils.buildMessage(RelayUtils.java:98) at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:72) at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:48) at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:149) at org.apache.synapse.rest.Resource.process(Resource.java:297).....................
Kindly guide me what i did wrong.
Set message builder correctly in the axis2.xml or remove the log mediator inside the inSequence.

wso2esb DOCTYPE declaration; expected a space between public and system

I have created an API that accepts a .xlsx file as POST input.
Code is mentioned below:
<api xmlns="http://ws.apache.org/ns/synapse" name="classroomcourse" context="/classroomcourse">
<resource methods="POST">
<inSequence>
<log level="full"/>
<property name="messageType" value="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" scope="axis2"/>
<send>
<endpoint>
<http uri-template="http://qa-aaa.ggg.com/xyz/api/importCourses"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<log level="full"/>
<property name="messageType" value="application/json" scope="axis2"/>
<send/>
</outSequence>
When I send request via POSTMAN and attach xlsx file then I get the below error:
[2016-03-02 17:32:13,332] ERROR - Error while building Passthrough stream {org.apache.synapse.transport.passthru.util.RelayUtils}
org.apache.axiom.om.OMException: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '"' (code 34) in DOCTYPE declaration; expected a space be
tween public and system identifiers
at [row,col {unknown-source}]: [1,50]
at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:296)
at org.apache.axiom.om.impl.llom.OMDocumentImpl.getOMDocumentElement(OMDocumentImpl.java:109)
at org.apache.axiom.om.impl.builder.StAXOMBuilder.getDocumentElement(StAXOMBuilder.java:570)
at org.apache.axiom.om.impl.builder.StAXOMBuilder.getDocumentElement(StAXOMBuilder.java:566)
at org.apache.synapse.transport.passthru.util.DeferredMessageBuilder.getDocument(DeferredMessageBuilder.java:149)
at org.apache.synapse.transport.passthru.util.RelayUtils.builldMessage(RelayUtils.java:133)
at org.apache.synapse.transport.passthru.util.RelayUtils.buildMessage(RelayUtils.java:98)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:72)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:48)
at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:149)
at org.apache.synapse.rest.Resource.process(Resource.java:297)
Kindly guide me how can I solve this issue.
Make sure that the right message builder is enabled in the axis2.xml file, for the Content type of the ESB inbound request.
Also try removing the <log level="full"/> in inSequence.

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.
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="kasun#gmail.com"/>
<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
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.