I have a below request (i am using WSO2 Enterprise integrator 6.5.0):
And I wanna get this in jsonpayload which is returned client
<ERROR_RESP>
<ERROR>
<ECODE>ST-VALS-002</ECODE>
<EDESC>Record Not Found for Branch Code-CHO:Currency 1-USD:Currency 2-MN</EDESC>
</ERROR>
<ERROR>
<ECODE>ST-SAVE-024</ECODE>
<EDESC>Failed to Query Data</EDESC>
</ERROR>
</ERROR_RESP>
It is my tried code to achieve it :
<foreach expression="json-eval($.ERROR_RESP.ERROR)">
<sequence>
<payloadFactory media-type="json">
<format>
{
"ErrorCode" : "$1",
"ErrorMessage" : "$2"
}
</format>
<args>
<arg evaluator="json" expression="$.ECODE"/>
<arg evaluator="json" expression="$.EDESC"/>
</args>
</payloadFactory>
<log level="full">
<property name="MESSAGE" value="ENDLOOP"/>
</log>
<loopback/>
</sequence>
</foreach>
enter code here
I did some search but nothings worked, I think foreach expression is not right.
Thanks
Regards,
In your sample the Loopback mediator is used. It will be used to move the message to the out flow (response path). Therefore, the Foreach mediator splits the message and sends the first message to the response path and end the flow.
You can prepare the XML payload and convert it to JSON using the messageType property with axis2 scope as follows.
<foreach expression="//ERROR">
<sequence>
<payloadFactory media-type="xml">
<format>
<ERROR xmlns="">
<ErrorCode>$1</ErrorCode>
<ErrorMessage>$2</ErrorMessage>
</ERROR>
</format>
<args>
<arg evaluator="xml" expression="//ECODE/text()"/>
<arg evaluator="xml" expression="//EDESC/text()"/>
</args>
</payloadFactory>
</sequence>
</foreach>
<property name="messageType" scope="axis2" value="application/json"/>
The final message will be like this:
{
"ERROR_RESP": {
"ERROR": [
{
"ErrorCode": "ST-VALS-002",
"ErrorMessage": "Record Not Found for Branch Code-CHO:Currency 1-USD:Currency 2-MN"
},
{
"ErrorCode": "ST-SAVE-024",
"ErrorMessage": "Failed to Query Data"
}
]
}
}
At the moment, foreach mediator in EI 6.5.0 does not support "json-eval()" expressions. This feature will be included in the upcoming versions.
As a workaround, you can use XPath inside the expression. You can use this blog as an example. https://medium.com/#Manuri/wso2-esb-foreach-mediator-example-87f041e2a912
Related
I am implementing failure sequence for a service in WSO2 EBS, but it just never return ERROR_MESSAGE or other failure properties like ERROR_REASON.
However it does return this fields inside $ctx:ERROR_RESPONSE.
how come is this possible? any clue how to extract this fields outside ERROR_RESPONSE?
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="Failure" statistics="enable" trace="enable" xmlns="http://ws.apache.org/ns/synapse">
<log description="Log error details in case of failure" level="full">
<property name="MESSAGE" value="An unexpected error occurred."/>
<property expression="$ctx:ERROR_CODE" name="ERROR_CODE"/>
<property expression="$ctx:ERROR_RESPONSE" name="ERROR_RESPONSE"/>
<property expression="$ctx:ERROR_MESSAGE" name="ERROR_MESSAGE"/>
</log>
<payloadFactory description="Build payload with error information" media-type="json">
<format>
{
"status": "An unexpected error occurred.",
"code": $1,
"response": $2,
"message": $3,
}
</format>
<args>
<arg evaluator="xml" expression="$ctx:ERROR_CODE"/>
<arg evaluator="xml" expression="$ctx:ERROR_RESPONSE"/>
<arg evaluator="xml" expression="$ctx:ERROR_MESSAGE"/>
</args>
</payloadFactory>
<respond description="Respond back to client with custom error payload"/>
</sequence>
I'm trying to create Message Mediation Policies, with which I can make an additional http call, process the response and enrich the current message. How can i do this? I use call Mediator, but I don’t understand how to handle the response.
<?xml version="1.0" encoding="UTF-8"?> <sequence name="call_out_handler" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<call blocking="true">
<endpoint>
<http method="get" uri-template="http://192.168.99.100:8888/stubFORAPIMan/ServletWithTimeout"/>
</endpoint>
</call> </sequence>
You can use the PayloadFactory Mediator [1] to handle/format the response you received by calling an endpoint inside the Call mediator.
An example would be like this. Say you want to provide a json object by populating the values from the response you received; you can define the json object format in the "format" section and populate the values by providing the arguments in the "args" section in the PayloadFactory mediator as below.
<payloadFactory media-type="json">
<format>
{
"Data": {
"PaymentSubmissionId": "$1",
"PaymentId": "$2",
"Status": "$3",
"CreationDateTime": "$4"
}
}
</format>
<args>
<arg evaluator="xml" expression="$body//PaymentSubId"/>
<arg evaluator="xml" expression="$body//PaymentId"/>
<arg evaluator="xml" value="AcceptedSettlementInProcess"/>
<arg value="2019-06-05T15:15:22+00:00"/>
</args>
</payloadFactory>
<property name="messageType" value="application/json" scope="axis2" type="STRING"/>
[1] https://docs.wso2.com/display/EI640/PayloadFactory+Mediator
In this case, how can I get the "MyString" content in WSO2 response?
Request:
<payloadFactory media-type="json">
<format>{
"Name" : "$1",
"group": "$3"
}
</format>
<args>
<arg evaluator="xml" expression="$ctx:Name"/>
<arg evaluator="xml" expression="$ctx:group"/>
</args>
</payloadFactory>
<call>
<endpoint key="ep_Server"/>
</call>
response:
Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><text xmlns="http://ws.apache.org/commons/ns/payload">{"MyString":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOjEsIlN5c3RlbUlkIjoyMSwiUHJvcGVydHlJZCI6OSwiSXNBZG1pbiI6ZmFsc2UsIkNyZWF0ZURhdGVUaW1lIjoiMjAxNy0xMS0wOFQxMDoyMjoxMi45MDA3MjE4KzA4OjAwIn0.k6FyUGwXOAeC63oGsPWz8ttwo1LeDG3vnTbw7dJ18GY"}</text></soapenv:Body></soapenv:Envelope>
Try this.
<log>
<property name="MyString" expression="json-eval($.MyString)"></property>
</log>
Ref: https://docs.wso2.com/display/ESB500/JSON+Support
In your case the output is not json it's xml containing a text, usually it indicate that you're using text/plain type, I think the issue is there.
Moreover your output is not matching the sample code you've
Could you try to put the following before your call:
<property name="messageType" scope="axis2" type="STRING" value="application/json"/>
What is the content type that the service you're calling is returning? Is it valid? If not it could be that wso2 ei is by default considering it's text
I have created a REST API using WSO2 API Manager (StockQuoteService) and configured a back end SOAP based web service (converting REST to SOAP) from where it will be getting the data based on the URL template.
In the "In Sequence", I have used a Switch mediator to send the request to different back end endpoints based on incoming data whereas in the default scenario (when no case match), I want to send the error message back to the client that the "Input message is invalid".
I have tried using the Send mediator, Respond Mediator, Sequence Mediator but still no success (may be doing something wrong) as still I am getting "no response from server" error when I try to invoke the URL which doesn't match any case of Switch and goes to Default.
How can I send the Error/Fault message back to the client from In Sequence of WSO2 API Manager?
In my scenario, the input sequence I used the switch mediator and I invoke an operation or another, in the default option I create my failure response
<inSequence>
<switch xmlns:xsd="http://pharmacy.arce.org/xsd"
description=""
source="//xsd:desc">
<case regex="NATURAL">
<log description="Search Pharmacy" level="custom" separator=",">
<property name="STATUS" value="Search Pharmacy"/>
</log>
<payloadFactory media-type="xml">
<format>
<p:searchpharmacy xmlns:p="http://pharmacy.arce.org">
<ax22:pharmacy xmlns:ax22="http://pharmacy.arce.org">
<xs:desc xmlns:xs="http://pharmacy.arce.org/xsd">$1</xs:desc>
<xs:id xmlns:xs="http://pharmacy.arce.org/xsd">$2</xs:id>
<xs:latitude xmlns:xs="http://pharmacy.arce.org/xsd">$3</xs:latitude>
<xs:longitude xmlns:xs="http://pharmacy.arce.org/xsd">$4</xs:longitude>
</ax22:pharmacy>
</p:searchpharmacy>
</format>
<args>
<arg evaluator="xml" expression="//xsd:desc"/>
<arg evaluator="xml" expression="//xsd:id"/>
<arg evaluator="xml" expression="//xsd:latitude"/>
<arg evaluator="xml" expression="//xsd:longitude"/>
</args>
</payloadFactory>
<header name="To" scope="default" value="urn:searchpharmacy"/>
<log level="full" separator=",">
<property name="Mensaje" value="Cuerpo"/>
</log>
</case>
<case regex="EXPERIMENTAL">
<log description="Search Pharmacy Direction" level="custom" separator=",">
<property name="STATUS" value="Search Pharmacy Direction Request"/>
</log>
<payloadFactory media-type="xml">
<format>
<p:searchPhone xmlns:p="http://pharmacy.arce.org">
<ax22:pharmacy xmlns:ax22="http://pharmacy.arce.org">
<xs:desc xmlns:xs="http://pharmacy.arce.org/xsd">$1</xs:desc>
<xs:id xmlns:xs="http://pharmacy.arce.org/xsd">$2</xs:id>
<xs:latitude xmlns:xs="http://pharmacy.arce.org/xsd">$3</xs:latitude>
<xs:longitude xmlns:xs="http://pharmacy.arce.org/xsd">$4</xs:longitude>
</ax22:pharmacy>
</p:searchPhone>
</format>
<args>
<arg evaluator="xml" expression="//xsd:desc"/>
<arg evaluator="xml" expression="//xsd:id"/>
<arg evaluator="xml" expression="//xsd:latitude"/>
<arg evaluator="xml" expression="//xsd:longitude"/>
</args>
</payloadFactory>
<header name="Action" scope="default" value="urn:searchPhone"/>
<property name="SOAPAction" scope="transport" type="STRING" value=""/>
<log level="full" separator=",">
<property name="Data" value="Body"/>
</log>
</case>
<default>
<log description="Fault" level="custom" separator=",">
<property name="STATUS" value="Invoke fault "/>
</log>
<payloadFactory media-type="xml">
<format>
<rs:fault xmlns:rs="http://pharmacy.arce.org">
<rs:code>-1</rs:code>
<rs:type>Invocation error</rs:type>
<rs:message>No operation has been invoked</rs:message>
<rs:description>The value of the input parameter is not valid</rs:description>
</rs:fault>
</format>
<args/>
</payloadFactory>
<respond/>
</default>
</switch>
</inSequence>
Here is the default section
<default>
<log description="Fault" level="custom" separator=",">
<property name="STATUS" value="Invoke fault "/>
</log>
<payloadFactory media-type="xml">
<format>
<rs:fault xmlns:rs="http://pharmacy.arce.org">
<rs:code>-1</rs:code>
<rs:type>Invocation error</rs:type>
<rs:message>No operation has been invoked</rs:message>
<rs:description>The value of the input parameter is not valid</rs:description>
</rs:fault>
</format>
<args/>
</payloadFactory>
<respond/>
</default>
And the fault response
{"fault":{"code":-1,"type":"Invocation error","message":"No operation has been invoked","description":"The value of the input parameter is not valid"}}
You can find other scenarios here
http://harshcreationz.blogspot.com/2016/02/common-and-error-handling-sequences.html
This should work.
<payloadFactory media-type="json">
<format>
{
"error":"true",
"message":"error case"
}
</format>
</payloadFactory>
<property name="messageType" value="application/json" scope="axis2"/>
<respond/>
Nits
The error response as comments "no response from server", I receive it when I call the api from the test console that has the API Manager.
You can see this link
API Console Issue
To validate the response you expect.
Try calling from another tool such as SOAP UI or Postman, in my case I receive a fault response message with the structure defined.
This is the call from the console API
curl -X GET --header 'Accept: application/xml' --header 'Authorization: Bearer 465f1385-a120-3c19-ad22-c3057e744a3b' 'https://169.254.193.10:8252/getById/1.0.0/getEmployeeXML?Id=5'
For the call from another client in the header pass these values you have in the api call
Accept: application/json
Authorization: Bearer 465f1385-a120-3c19-ad22-c3057e744a3b'
I have this below mentioned soap request, I am using WSO2ESB 4.9.
Soap request :
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><jsonArray><jsonElement><a><s>as</s></a><b>Type1</b><c>C1</c><d><t>A1</t></d><e>e1</e></jsonElement><jsonElement><a><s>as</s></a><b>Type2</b><c>C2</c><d><t>A1</t></d><e>e1</e></jsonElement></jsonArray></soapenv:Body></soapenv:Envelope>
I want to iterator over jsonElement/b and get json list : ["Type1", "Type2"].But I am getting below list :
[
"\n\t\t\t\t\t\t\t\t\t\t\tType1\n\t\t\t\t\t\t\t\t\t",
"\n\t\t\t\t\t\t\t\t\t\t\tType2\n\t\t\t\t\t\t\t\t\t"
]
Please any way to get the required list.For each mediator :
<foreach expression="//jsonArray/jsonElement">
<sequence>
<payloadFactory media-type="xml">
<format>
<jsonElement>
$1
</jsonElement>
</format>
<args>
<arg expression="//b" evaluator="xml" />
</args>
</payloadFactory>
</sequence>
</foreach>
Got it working, there is space and newline in foreach <jsonElement>.
Working code:
<foreach expression="//jsonArray/jsonElement">
<sequence>
<payloadFactory media-type="xml">
<format>
<jsonElement xmlns="">$1</jsonElement>
</format>
<args>
<arg expression="//b" evaluator="xml" />
</args>
</payloadFactory>
</sequence>
</foreach>