WSO2 EI 6.6.0 Class Mediator not being able to use SOAP call return content - wso2

I have the following API in EI 6.6.0:
<?xml version='1.0' encoding='UTF-8'?>
<api xmlns="http://ws.apache.org/ns/synapse" name="sample" context="/sample">
<resource methods="POST">
<inSequence>
<payloadFactory media-type="xml">
<format>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<P xmlns="http://tempuri.org/">
<P1>$1</P1>
<P2>$2</P2>
<P3>$3</P3>
</P>
</soap:Body>
</soap:Envelope>
</format>
<args>
<arg evaluator="json" expression="$.p1" />
<arg evaluator="json" expression="$.p2" />
<arg evaluator="json" expression="$.p3" />
</args>
</payloadFactory>
<log level="full" />
<property name="Content-Type" value="text/xml;charset=UTF-8" scope="axis2"/>
<header name="Accept" scope="transport" value="text/xml"/>
<call>
<endpoint>
<wsdl Action="name_of_the_action" service="name_of_the_service" port="name_of_soap_port" uri="http://<ip>/path?WSDL" />
</endpoint>
</call>
<class name="my_mediator_package"></class>
<log level="full" />
<payloadFactory media-type="xml">
<format>
<retorno xmlns="">
<msg>$1</msg>
</retorno>
</format>
<args>
<arg evaluator="xml" expression="get-property('property_set_on_mediator')" />
</args>
</payloadFactory>
<property name="messageType" value="application/xml" scope="axis2" type="STRING" />
<respond />
</inSequence>
<outSequence>
</outSequence>
<faultSequence>
<property name="text" value="An unexpected error occured"/>
<property name="message" expression="get-property('ERROR_MESSAGE')"/>
<payloadFactory media-type="xml">
<format>
<error xmlns="">
<msg>$1</msg>
</error>
</format>
<args>
<arg evaluator="xml" expression="get-property('ERROR_MESSAGE')"/>
</args>
</payloadFactory>
<property name="messageType" value="application/json" scope="axis2" type="STRING"/>
<respond/>
</faultSequence>
</resource>
</api>
my mediate method content:
public boolean mediate(MessageContext synCtx) {
org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) synCtx)
.getAxis2MessageContext();
try {
// Getting the json payload to string
String jsonPayloadToString = JsonUtil.jsonPayloadToString(((Axis2MessageContext) synCtx)
.getAxis2MessageContext());
System.out.println("original payload : \n" + jsonPayloadToString + "\n");
I'm not being able to use the return from my SOAP call in my mediator so I can work on it.
When I run the API I get the following from my mediator code:
original payload:
{}
Is there a way so I can obtain the SOAP call returned envelope and use it in my mediator?

The JSON payload is coming as empty because you are calling a SOAP backend and getting a SOAP payload. You can use synCtx.getEnvelope() in your mediator to get the SOAPEnvelope from the response.

1- make sure the json payload is there. So, log the json properties inside your inSequence.
2- I'm not being able to use the return from my SOAP call in my mediator so I can work on it. you can see the response payload in your outSequence which currently is doing nothing.
3- According to your scenario which is simply calling a SOAP webservice, you do not need a class mediator. In other words, when you do not need manipulating the initial payload and then pass it to the destination service, logically implementing your own class mediator benefits you nothing.
Also, there are quite number of samples in https://docs.wso2.com/display/EI611 which will help you.
Please let me know if your problem is solved.

Related

how to convert xml to json in wso2

Scenario
I've implemented an API which makes a soap call to external service. I need to convert the response from xml to json (which is actually happening). But somehow I'm unable to convert the nested xml nodes to json.
API
<?xml version="1.0" encoding="UTF-8"?>
<api context="/nadra" name="NadraVerification" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST">
<inSequence>
<payloadFactory media-type="xml">
<format>
<soapenv:Envelope xmlns:nad="http://XXXX.Verification" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<nad:GetCitizenDemographics>
<nad:franchizeID>XXX</nad:franchizeID>
<nad:xml_request_data><![CDATA[
<CITIZEN_VERIFICATION><USER_VERIFICATION><USERNAME>XXXX</USERNAME><PASSWORD>XXXX</PASSWORD></USER_VERIFICATION><REQUEST_DATA><TRANSACTION_ID>$4</TRANSACTION_ID><SESSION_ID></SESSION_ID><CITIZEN_NUMBER>$5</CITIZEN_NUMBER><CONTACT_NUMBER>$6</CONTACT_NUMBER><AREA_NAME>$7</AREA_NAME></REQUEST_DATA></CITIZEN_VERIFICATION>
]]></nad:xml_request_data>
</nad:GetCitizenDemographics>
</soapenv:Body>
</soapenv:Envelope>
</format>
<args>
<arg evaluator="json" expression="$.transactionId"/>
<arg evaluator="json" expression="$.citizenNo"/>
<arg evaluator="json" expression="$.contactNo"/>
<arg evaluator="json" expression="$.areaName"/>
</args>
</payloadFactory>
<header name="Action" scope="default" value="http://XXXX.XXXX.XXXX/ICitizenVerification/GetCitizenDemographics"/>
<send>
<endpoint>
<address format="soap11" uri="https://verification.nadra.gov.pk/bioverisys/xml">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>1</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</address>
</endpoint>
</send>
</inSequence>
<outSequence>
<property name="messageType" scope="axis2" type="STRING" value="application/json"/>
<payloadFactory media-type="json">
<format>
{
"status" : "success"
"response": $1
}</format>
<args>
<arg evaluator="json" expression="$.GetCitizenDemographicsResponse.GetCitizenDemographicsResult"/>
</args>
</payloadFactory>
<respond/>
</outSequence>
<faultSequence>
<payloadFactory media-type="json">
<format>
{
"status" : "failure"
}</format>
<args/>
</payloadFactory>
</faultSequence>
</resource>
</api>
Response from External Service after converting XML to JSON
{
"GetCitizenDemographicsResponse": {
"GetCitizenDemographicsResult": "<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<CITIZEN_VERIFICATION xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n <RESPONSE_DATA>\r\n <RESPONSE_STATUS>\r\n <CODE>100</CODE>\r\n
<MESSAGE>successful</MESSAGE>\r\n </RESPONSE_STATUS>\r\n <SESSION_ID>00001343681312</SESSION_ID>\r\n
<CITIZEN_NUMBER>XXXX</CITIZEN_NUMBER>\r\n <PERSON_DATA>\r\n <NAME>ایوب جمال</NAME>\r\n
<FATHER_HUSBAND_NAME>XXX</FATHER_HUSBAND_NAME>\r\n <PRESENT_ADDRESS>XXXX</PRESENT_ADDRESS>\r\n
<PERMANANT_ADDRESS>XXXX</PERMANANT_ADDRESS>\r\n <DATE_OF_BIRTH>1996-01-10</DATE_OF_BIRTH>\r\n
<GENDER>male</GENDER>\r\n <EXPIRY_DATE>2030-07-11</EXPIRY_DATE>\r\n </PERSON_DATA>\r\n
</RESPONSE_DATA>\r\n</CITIZEN_VERIFICATION>"
}
}
Actual Response
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GetCitizenDemographicsResponse xmlns="http://NADRA.Citizen.Verification"><GetCitizenDemographicsResult><?xml version="1.0" encoding="utf-16"?>
<CITIZEN_VERIFICATION xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<RESPONSE_DATA>
<RESPONSE_STATUS>
<CODE>107</CODE>
<MESSAGE>invalid citizen nummber</MESSAGE>
</RESPONSE_STATUS>
<SESSION_ID />
<CITIZEN_NUMBER>$5</CITIZEN_NUMBER>
</RESPONSE_DATA>
</CITIZEN_VERIFICATION></GetCitizenDemographicsResult></GetCitizenDemographicsResponse></s:Body></s:Envelope>
Question
My exact question is how can I convert the nested xml to json as well?. Since it has converted the first two xml nodes but the third xml node hasn't been converted.
Since you are using the Payload Factory to modify the response, you can use an XPath expression to extract the values from the soap endpoint. You don't need to explicitly convert the SOAP response to JSON using the messageType property before the Payload Factory.
Remove the messageType property
Use an XPath expression in the Payload Factory
Update Answer
According to the response from the backend, in the SOAP body, it has declared the <?xml version="1.0" encoding="UTF-8"?> tag before the CITIZEN_VERIFICATION element. Because of this, we need to first assign it to a property with OM type and then use it in the payload factory. Since we have a namespace defined for GetCitizenDemographicsResponse we need to add the namespace tag as well to the property mediator.
Option 1: With namespace
<property type="OM" xmlns:ns="http://NADRA.Citizen.Verification" expression="//ns:GetCitizenDemographicsResponse/*" name="Payload"/>
Or you can use local-name() function to get the value without defining the namespace in property mediator.
Option 2: Without defining the namespace
<property type="OM" expression="//*[local-name()='GetCitizenDemographicsResult']" name="Payload"/>
<outSequence>
<property type="OM" xmlns:ns="http://NADRA.Citizen.Verification" expression="//ns:GetCitizenDemographicsResponse/*" name="Payload"/>
<payloadFactory media-type="json">
<format>
{
"status" : "success",
"response": $1
}
</format>
<args>
<arg expression="$ctx:Payload" />
</args>
</payloadFactory>
<respond/>
</outSequence>

How to use the in sequence and out sequence to a custom response in WSO2 APIM?

I am using WSO2 APIM 2.1.0 and IS 5.3.0
I'm currently trying to create an API that registers a certain user by calling the admin service UserInformationRecoveryService which gives out a custom JSON response if the creation is successful and another response if it is unsuccessful, in which case the user already exists.
So far I have written the in sequence and the out sequence as follows but I am having trouble getting the expected output.(The success response is always seen even when the user already exists. That is, the else block is getting executed in the out sequence.)
In Sequence
<sequence xmlns="http://ws.apache.org/ns/synapse" name="registerTestHrisIn">
<log level="full" />
<property name="userName" expression="//userName" scope="default" type="STRING" />
<property name="password" expression="//password" scope="default" type="STRING" />
<property name="givenname" expression="//givenname" scope="default" type="STRING" />
<property name="lastname" expression="//lastname" scope="default" type="STRING" />
<property name="emailaddress" expression="//emailaddress" scope="default" type="STRING" />
<property name="organization" expression="//organization" scope="default" type="STRING" />
<property name="telephone" expression="//telephone" scope="default" type="STRING" />
<property name="authorization" expression="get-property('transport', 'Authorization')" scope="default" type="STRING" />
<payloadFactory media-type="xml">
<format>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ser="http://services.mgt.identity.carbon.wso2.org" xmlns:xsd="http://dto.mgt.identity.carbon.wso2.org/xsd">
<soap:Header />
<soap:Body>
<ser:registerUser>
<ser:userName>$1</ser:userName>
<ser:password>$2</ser:password>
<ser:claims>
<xsd:claimUri>http://wso2.org/claims/givenname</xsd:claimUri>
<xsd:claimValue>$3</xsd:claimValue>
</ser:claims>
<ser:claims>
<xsd:claimUri>http://wso2.org/claims/lastname</xsd:claimUri>
<xsd:claimValue>$4</xsd:claimValue>
</ser:claims>
<ser:claims>
<xsd:claimUri>http://wso2.org/claims/emailaddress</xsd:claimUri>
<xsd:claimValue>$5</xsd:claimValue>
</ser:claims>
<ser:claims>
<xsd:claimUri>http://wso2.org/claims/organization</xsd:claimUri>
<xsd:claimValue>$6</xsd:claimValue>
</ser:claims>
<ser:claims>
<xsd:claimUri>http://wso2.org/claims/telephone</xsd:claimUri>
<xsd:claimValue>$7</xsd:claimValue>
</ser:claims>
<ser:profileName>default</ser:profileName>
<ser:tenantDomain>carbon.super</ser:tenantDomain>
</ser:registerUser>
</soap:Body>
</soap:Envelope>
</format>
<args>
<arg evaluator="xml" expression="$ctx:userName" />
<arg evaluator="xml" expression="$ctx:password" />
<arg evaluator="xml" expression="$ctx:givenname" />
<arg evaluator="xml" expression="$ctx:lastname" />
<arg evaluator="xml" expression="$ctx:emailaddress" />
<arg evaluator="xml" expression="$ctx:organization" />
<arg evaluator="xml" expression="$ctx:telephone" />
</args>
</payloadFactory>
</sequence>
Out Sequence
<sequence xmlns="http://ws.apache.org/ns/synapse" name="registerTestHrisOut">
<property xmlns:ns="http://services.mgt.identity.carbon.wso2.org" name="registerUserResponse" expression="//ns:registerUserResponse/ns:return"/>
<log level="full">
<property name="registerUserResponse" expression="registerUserResponse"/>
</log>
<filter source="get-property('registerUserResponse')" regex=".*18003">
<then>
<payloadFactory media-type="json">
<format>{"ServiceResponse": {"Code": "1","Status": "false","Message": " Failed"}}</format>
</payloadFactory>
</then>
<else>
<payloadFactory media-type="json">
<format>{"ServiceResponse": {"Code": "1","Status": "true","Message": " Success"}}</format>
</payloadFactory>
</else>
</filter>
</sequence>
Error response which needs to be caught
<soapenv:Body>
<ns:registerUserResponse xmlns:ns="http://services.mgt.identity.carbon.wso2.org">
<ns:return xmlns:ax2354="http://mgt.identity.carbon.wso2.org/xsd" xmlns:ax2355="http://base.identity.carbon.wso2.org/xsd" xmlns:ax2358="http://beans.mgt.identity.carbon.wso2.org/xsd" xmlns:ax2359="http://dto.mgt.identity.carbon.wso2.org/xsd" xmlns:ax2360="http://mail.mgt.identity.carbon.wso2.org/xsd" xmlns:ax2364="http://beans.mgt.captcha.carbon.wso2.org/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax2358:VerificationBean">
<ax2358:error>18003 Username 'TEST.COM/testUser' already exists in the system. Please pick another username.</ax2358:error>
<ax2358:key xsi:nil="true" />
<ax2358:notificationData xsi:nil="true" />
<ax2358:redirectPath xsi:nil="true" />
<ax2358:userId xsi:nil="true" />
<ax2358:verified>false</ax2358:verified>
</ns:return>
</ns:registerUserResponse>
</soapenv:Body>
</soapenv:Envelope>
Any workaround to achieve the expected response is highly appreciated.
Thanks in advance.
Let's revamp the sequences and try the scenarios.
Perform the following changes to extract the correct error message from the response and to validate in the Filter
Update the property mediator in the out-sequence as following to specify the path up to the leaf node to extract the error message
<property
xmlns:ns="http://services.mgt.identity.carbon.wso2.org"
xmlns:xsd="http://beans.mgt.identity.carbon.wso2.org/xsd"
name="registerUserResponse"
expression="//ns:registerUserResponse/ns:return/xsd:error"
/>
Update the regex pattern in the Filter mediator as following (.*18003 to .*18003.*: this is to define that after the mentioned key, there will be a set of strings to be matched)
<filter source="get-property('registerUserResponse')" regex=".*18003.*">
<then>
...
</then>
<else>
...
</else>
</filter>
Please update the sequences as mentioned, deploy, and try the scenario.

getting cognito acces_token with wso2 esb

I'm trying to get access token through AWS Cognito with client credentials but getting something else.
I'm doing this in wso2 Enterprise integrator 6.1.0
<payloadFactory media-type="xml">
<format>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema/instance">
<soapenv:Header>
<Content-Type xmlns="">$1</Content-Type>
<Authorization xmlns="">$2</Authorization>
</soapenv:Header>
<soapenv:Body/>
</soapenv:Envelope>
</format>
<args>
<arg evaluator="xml" value="application/x-www-form-urlencoded"/>
<arg value="Basic 2354sdfmdtrerkdfdgkeryryrtwdasr345345twsdfwsedtr34"/>
</args>
</payloadFactory>
<log level="full"/>
<property name="DISABLE_CHUNKING" scope="axis2" type="STRING" value="true"/>
<endpoint key="validateUser-ext-ep"/>
</call>
<log level="full"/>
after this the response i'm getting is like this :
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
<ns:binary xmlns:ns="http://ws.apache.org/commons/ns/payload">PCFET0NUWVBFIGh0bWw+CjxodG1sIGxhbiYWNrZ3JvdW5kLWN1c3RvbWl6YWJsZSBtb2RhbC1jb250ZW50LW1vYmlsZSI+CiAgICAgICAgICAgICAgICA8ZGl2PgogICAgICAgICAgICAgICAgICAgIDxkaXYgY2xhc3M9ImJhbm5lci1jdXN0b21pemFibGUiPgogICAgICAgICAgICAgICAgICAgICAgICA8Y2VudGVyPgogICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgIDwvY2VudGVyPgogICAgICAgICAgICAgICAgICAgIDwvZGl2PgogICAgICAgICAgICAgICAgPC9kaXY+CiAgICAgICAgICAgICAgICA8ZGl2IGNsYXNzPSJtb2RhbC1ib2R5Ij4KICAgICAgICAgICAgICAgICAgICA8cCBhbGlnbj0iY2VudGVyIj5BbiBlcnJvciB3YXMgZW5jb3VudGVyZWQgd2l0aCB0aGUgcmVxdWVzdGVkIHBhZ2UuPC9wPgogICAgICAgICAgICAgICAgICAgIDxicj4KICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgIDwvZGl2PgogICAgICAgICAgICA8L2Rpdj4KICAgICAgICA8L2Rpdj4KICAgIDwvZGl2Pgo8L2JvZHk+Cgo8L2h0bWw+Cg==</ns:binary>l
</soapenv:Body>
</soapenv:Envelope>
I don't know if I'm doing something wrong as in postman I sent the data same way I was getting the token there I passed the Authrization as Basic dfudne4r49859dfnw34598sdfs base64 endcoded(client:client_secret) and Content-Type : application/x-www-form-urlencoded in header and in params I passed grant_type: client_credential for this I was able to get the token but when I tried in wso2 esb I got the above error
the endpoint looks like :https://xxxxxxxxxx.us-east-1.amazoncognito.com/oauth2/token?grant_type=client_credentials
this is the API to get the Access token and it returns the access token
<?xml version="1.0" encoding="UTF-8"?>
<api context="/api/myService" name="my-service-api" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST" uri-template="/getToken">
<inSequence>
<script language="js"><![CDATA[var payload = mc.getPayloadXML();
var log = mc.getServiceLog();
var client_id = payload..*::client_id.toString();
var client_secret = payload..*::client_secret.toString();
mc.setProperty("client_id", client_id);
mc.setProperty("client_secret", client_secret);]]>
</script>
<payloadFactory media-type="json">
<format/>
<args/>
</payloadFactory>
<property name="ContentType" scope="axis2" type="STRING" value="application/x-www-form-urlencoded"/>
<property expression="fn:concat($ctx:client_id,':',$ctx:client_secret)" name="credentials" scope="default" type="STRING"/>
<property expression="fn:concat('Basic ', base64Encode($ctx:credentials))" name="Authorization" scope="transport" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>
<property name="FORCE_POST_PUT_NOBODY" scope="axis2" type="BOOLEAN" value="true"/>
<property name="DISABLE_CHUNKING" scope="axis2" type="STRING" value="true"/>
<call>
<endpoint>
<http method="post" uri-template="https://xxxxxxxxxxx.amazoncognito.com/oauth2/token?grant_type=client_credentials"/>
</endpoint>
</call>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
Call the above API like this :
<payloadFactory media-type="xml">
<format>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<root>
<client_id>$1</client_id>
<client_secret>$2</client_secret>
</root>
</soapenv:Body>
</soapenv:Envelope>
</format>
<args>
<arg evaluator="xml" expression="get-property('client_id')"/>
<arg evaluator="xml" expression="get-property('client_secret')"/>
</args>
</payloadFactory>
<property name="ContentType" scope="default" type="STRING" value="application/xml"/>
<property action="remove" name="REST_URL_POSTFIX" scope="axis2"/>
<call>
<endpoint>
<http method="post" uri-template="http://localhost:8280/api/myService/getToken"/>
</endpoint>
</call>
<script language="js">
<![CDATA[ var tokenContainer = mc.getPayloadJSON();
var log = mc.getServiceLog();
mc.setProperty("Authorization_header", tokenContainer.access_token);]]>
</script>
The Response will be
{"access_token":"LzSq94JoaUT2LwJlkEl35CXX0MdwqtUKIL8Wvi7dm4SqcSofR4xF5xBZre83MZXpHOr-Hg","expires_in":360,"token_type":"Bearer"}
In last script mediator you can access the token Access token throught getting it by mc.getPayloadJSON() this will give the same response as shwon above

WSO2 API manager - How to send Error/Fault message back to the Client from InSequence

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'

Set Custom Payload in wso2 ESB or AM?

I have a rest API in ESB server.I need to set payload as follows
<fields>Name</fields>
<query>
<term>
<NUM>100</NUM>
</term>
</query>
any suggestion ?
you could use the payloadfactory mediator in ESB 4.8.1, take this xml as an example. you need to do some changes :-) but it´s an start:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="JsonToXMLProxy"
transports="https http local"
startOnLoad="true"
trace="disable">
<description/>
<target>
<endpoint>
<address uri="http://www.w3schools.com/webservices/tempconvert.asmx" format="soap11"/>
</endpoint>
<inSequence>
<log>
<property name="TEMPERATURA_ENTRADA" expression="json-eval($.celsius)"/>
</log>
<payloadFactory media-type="xml">
<format>
<web:CelsiusToFahrenheit xmlns:web="http://www.w3schools.com/webservices/">
<web:Celsius>$1</web:Celsius>
</web:CelsiusToFahrenheit>
</format>
<args>
<arg evaluator="json" expression="$.celsius"/>
</args>
</payloadFactory>
<header name="Action"
value="http://www.w3schools.com/webservices/CelsiusToFahrenheit"/>
</inSequence>
<outSequence>
<log>
<property xmlns:p="http://www.w3schools.com/webservices/"
name="TEMPERATURA_SALIDA"
expression="//p:CelsiusToFahrenheitResponse/p:CelsiusToFahrenheitResult"/>
</log>
<payloadFactory media-type="json">
<format>
"Temperatura" : {
"EnFahrenheit" : $1
}
</format>
<args>
<arg xmlns:p="http://www.w3schools.com/webservices/"
evaluator="xml"
expression="//p:CelsiusToFahrenheitResponse/p:CelsiusToFahrenheitResult"/>
</args>
</payloadFactory>
<property name="messageType" value="application/json" scope="axis2"/>
<send/>
</outSequence>
</target>
</proxy>
Try payload/enrich mediators ..